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 __());
    };
})();
import * as React from 'react';
import { Radio } from '../form/Radio';
import { TextInput } from '../form/TextInput';
import { Form } from '../form/Form';
import { Highlighter } from '../Highlighter';
import { PropertyFilterTitle } from './PropertyFilterTitle';
import { propertyFieldContext } from './context';
var PropertyFilterSelect = /** @class */ (function (_super) {
    __extends(PropertyFilterSelect, _super);
    function PropertyFilterSelect() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.state = {
            searchQuery: ''
        };
        _this.fieldID = (Math.random() * 1000000).toFixed(0);
        _this.handleSearchChange = function (event) {
            _this.setState({
                searchQuery: event.target.value
            });
        };
        _this.renderOption = function (option, index) {
            var searchQuery = _this.state.searchQuery;
            var searchRegExp = new RegExp(searchQuery, 'i');
            return (React.createElement(Radio, { noOffset: true, className: "rc-property-filter__section-item", "data-test-automation-id": "item-" + index, key: option.id, name: _this.fieldID, value: option.id, disabled: option.disabled },
                React.createElement(Highlighter, { highlightRegexp: searchRegExp }, option.title)));
        };
        _this.renderRoot = function (context) {
            var _a;
            var _b = _this.props, id = _b.id, title = _b.title, testAutomationId = _b.testAutomationId;
            var value = context.values[id] || '';
            var onChange = context.onChange;
            var formValue = (_a = {}, _a[_this.fieldID] = value, _a);
            var changeHandler = function (formData) {
                onChange(id, formData[_this.fieldID]);
            };
            return (React.createElement("section", { className: "rc-property-filter__section", "data-test-automation-id": testAutomationId },
                React.createElement(Form, { noContainer: true, value: formValue, onChange: changeHandler },
                    React.createElement(PropertyFilterTitle, { key: "title" }, title),
                    _this.renderSearch(),
                    React.createElement("div", { className: "rc-property-filter__section-container" }, _this.renderOptions()))));
        };
        _this.renderOptions = function () {
            var _a = _this.props, options = _a.options, noResultsLabel = _a.noResultsLabel;
            var searchQuery = _this.state.searchQuery;
            var optionNodes = [];
            for (var index = 0; index < options.length; index++) {
                if (!searchQuery ||
                    options[index].title
                        .toString()
                        .toLowerCase()
                        .includes(searchQuery.toLowerCase())) {
                    optionNodes.push(_this.renderOption(options[index], index));
                }
            }
            if (optionNodes.length === 0) {
                return React.createElement("div", { className: "rc-property-filter__no-results" }, noResultsLabel);
            }
            return optionNodes;
        };
        return _this;
    }
    PropertyFilterSelect.prototype.renderSearch = function () {
        var _a = this.props, id = _a.id, hasSearch = _a.hasSearch, searchLabel = _a.searchLabel;
        var searchQuery = this.state.searchQuery;
        if (!hasSearch) {
            return null;
        }
        return (React.createElement(TextInput, { className: "rc-property-filter__search", name: "filterSearch-" + id, placeholder: searchLabel, value: searchQuery, onChange: this.handleSearchChange }));
    };
    PropertyFilterSelect.prototype.render = function () {
        return React.createElement(propertyFieldContext.Consumer, null, this.renderRoot);
    };
    PropertyFilterSelect.contextType = propertyFieldContext;
    PropertyFilterSelect.defaultProps = {
        searchLabel: 'Search',
        noResultsLabel: 'No results',
        testAutomationId: 'propertyFilterSelect'
    };
    return PropertyFilterSelect;
}(React.PureComponent));
export { PropertyFilterSelect };
//# sourceMappingURL=PropertyFilterSelect.js.map