import * as tslib_1 from "tslib";
import { StringColumn } from '../../model';
import { filterMissingMarkup, findFilterMissing } from '../missing';
import ADialog from './ADialog';
import { updateFilterState } from './utils';
import { cssClass } from '../../styles';
/** @internal */
var StringFilterDialog = /** @class */ (function (_super) {
    tslib_1.__extends(StringFilterDialog, _super);
    function StringFilterDialog(column, dialog) {
        var _this = _super.call(this, dialog, {
            fullDialog: true
        }) || this;
        _this.column = column;
        return _this;
    }
    StringFilterDialog.prototype.updateFilter = function (filter) {
        updateFilterState(this.attachment, this.column, filter != null && filter !== '');
        this.column.setFilter(filter);
    };
    StringFilterDialog.prototype.reset = function () {
        this.findInput('input[type="text"]').value = '';
        this.forEach('input[type=checkbox]', function (n) { return n.checked = false; });
        this.updateFilter(null);
    };
    StringFilterDialog.prototype.submit = function () {
        var filterMissing = findFilterMissing(this.node).checked;
        if (filterMissing) {
            this.updateFilter(StringColumn.FILTER_MISSING);
            return true;
        }
        var input = this.findInput('input[type="text"]').value;
        var isRegex = this.findInput('input[type="checkbox"]').checked;
        this.updateFilter(isRegex ? new RegExp(input) : input);
        return true;
    };
    StringFilterDialog.prototype.build = function (node) {
        var _this = this;
        var bak = this.column.getFilter() || '';
        var bakMissing = bak === StringColumn.FILTER_MISSING;
        if (bakMissing) {
            bak = '';
        }
        node.insertAdjacentHTML('beforeend', "<input type=\"text\" placeholder=\"Filter " + this.column.desc.label + "...\" autofocus value=\"" + ((bak instanceof RegExp) ? bak.source : bak) + "\" style=\"width: 100%\">\n    <label class=\"" + cssClass('checkbox') + "\"><input type=\"checkbox\" " + ((bak instanceof RegExp) ? 'checked="checked"' : '') + "><span>Use regular expressions</span></label>\n    " + filterMissingMarkup(bakMissing));
        var filterMissing = findFilterMissing(node);
        var input = node.querySelector('input[type="text"]');
        var isRegex = node.querySelector('input[type="checkbox"]');
        var update = function () {
            input.disabled = filterMissing.checked;
            isRegex.disabled = filterMissing.checked;
            if (filterMissing.checked) {
                _this.updateFilter(StringColumn.FILTER_MISSING);
                return;
            }
            var valid = input.value.trim();
            filterMissing.disabled = valid.length > 0;
            if (valid.length <= 0) {
                _this.updateFilter(null);
                return;
            }
            _this.updateFilter(isRegex.checked ? new RegExp(input.value, 'gm') : input.value);
        };
        filterMissing.onchange = update;
        input.onchange = update;
        isRegex.onchange = update;
    };
    return StringFilterDialog;
}(ADialog));
export default StringFilterDialog;
//# sourceMappingURL=StringFilterDialog.js.map