"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
    result["default"] = mod;
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
var React = __importStar(require("react"));
var Checkbox_1 = require("../form/Checkbox");
var PropertyFilterTitle_1 = require("./PropertyFilterTitle");
var context_1 = require("./context");
var PropertyFilterMultiSelect = /** @class */ (function (_super) {
    __extends(PropertyFilterMultiSelect, _super);
    function PropertyFilterMultiSelect() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.getValues = function (context) {
            var id = _this.props.id;
            var values = context.values[id] || [];
            if (typeof values === 'string') {
                return [];
            }
            return values;
        };
        _this.renderOption = function (option, index, values, onChange) {
            var checked = values.includes(option.id);
            var changeHandler = function () { return onChange(option.id, !checked); };
            return (React.createElement(Checkbox_1.Checkbox, { className: "rc-property-filter__section-item", "data-test-automation-id": "item-" + index, key: option.id, name: option.id, checked: checked, disabled: option.disabled, onChange: changeHandler }, option.title));
        };
        _this.renderRoot = function (context) {
            var _a = _this.props, id = _a.id, title = _a.title, options = _a.options, testAutomationId = _a.testAutomationId;
            var values = _this.getValues(context);
            var onChange = context.onChange;
            var renderOption = function (option, index) {
                return _this.renderOption(option, index, values, function (checkboxId, value) {
                    var nextValue = new Set(values);
                    if (value) {
                        nextValue.add(checkboxId);
                    }
                    else {
                        nextValue.delete(checkboxId);
                    }
                    onChange(id, Array.from(nextValue));
                });
            };
            return (React.createElement("section", { className: "rc-property-filter__section", "data-test-automation-id": testAutomationId },
                React.createElement(PropertyFilterTitle_1.PropertyFilterTitle, null, title),
                React.createElement("div", { className: "rc-property-filter__section-container" }, options.map(renderOption))));
        };
        return _this;
    }
    PropertyFilterMultiSelect.prototype.render = function () {
        return React.createElement(context_1.propertyFieldContext.Consumer, null, this.renderRoot);
    };
    PropertyFilterMultiSelect.contextType = context_1.propertyFieldContext;
    PropertyFilterMultiSelect.defaultProps = {
        testAutomationId: 'propertyFilterMultiSelect'
    };
    return PropertyFilterMultiSelect;
}(React.PureComponent));
exports.PropertyFilterMultiSelect = PropertyFilterMultiSelect;
//# sourceMappingURL=PropertyFilterMultiSelect.js.map