"use strict";

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.ConditionFilter = void 0;

const styles_1 = require("./styles"), types_1 = require("./types"), constant_1 = require("./constant");

class ConditionFilter {
    constructor(table, filterStateManager, pluginOptions) {
        this.currentCategory = types_1.FilterOperatorCategory.ALL, this.categories = [], 
        this.operators = [], this.table = table, this.filterStateManager = filterStateManager, 
        this.pluginOptions = pluginOptions, this.styles = pluginOptions.styles || {}, this.categories = pluginOptions.conditionCategories, 
        this.operators = constant_1.operators;
    }
    setSelectedField(fieldId) {
        this.selectedField = fieldId, this.updateOperatorOptions(), this.loadCurrentFilterState();
    }
    updateOperatorOptions() {
        if (!this.operatorSelect) return;
        let filteredOperators;
        if (this.operatorSelect.innerHTML = "", this.currentCategory === types_1.FilterOperatorCategory.ALL) {
            const uniqueOperators = new Map;
            this.operators.filter((op => this.categories.map((cat => cat.value)).includes(op.category))).forEach((op => {
                uniqueOperators.has(op.value) || uniqueOperators.set(op.value, op);
            })), filteredOperators = Array.from(uniqueOperators.values());
        } else filteredOperators = this.operators.filter((op => op.category === this.currentCategory));
        filteredOperators.forEach((op => {
            const option = document.createElement("option");
            option.value = op.value, option.textContent = op.label, this.operatorSelect.appendChild(option);
        }));
    }
    handleCategoryChange() {
        this.categorySelect && (this.currentCategory = this.categorySelect.value, this.updateOperatorOptions(), 
        this.syncUIState());
    }
    loadCurrentFilterState() {
        const filter = this.filterStateManager.getFilterState(this.selectedField);
        filter && "byCondition" === filter.type ? (filter.operator && this.operatorSelect && (this.operatorSelect.value = filter.operator), 
        void 0 !== filter.condition && this.valueInput && (Array.isArray(filter.condition) ? (this.valueInput.value = String(filter.condition[0]), 
        this.valueInputMax && (this.valueInputMax.value = String(filter.condition[1]))) : (this.valueInput.value = String(filter.condition), 
        this.valueInputMax && (this.valueInputMax.value = ""))), this.syncUIState()) : this.reset();
    }
    reset() {
        this.operatorSelect && (this.operatorSelect.selectedIndex = 0), this.valueInput && (this.valueInput.value = "", 
        this.valueInput.placeholder = "请输入筛选值"), this.valueInputMax && (this.valueInputMax.value = ""), 
        this.currentCategory = types_1.FilterOperatorCategory.ALL, this.categorySelect && (this.categorySelect.value = types_1.FilterOperatorCategory.ALL), 
        this.updateOperatorOptions(), this.syncUIState();
    }
    syncUIState() {
        if (!this.operatorSelect || !this.valueInput) return;
        const selectedOperator = this.operatorSelect.value, andLabel = this.valueInput.nextElementSibling;
        this.isRangeOperator(selectedOperator) ? (this.valueInput.placeholder = "最小值", this.valueInputMax && (this.valueInputMax.style.display = "inline-block"), 
        andLabel && (andLabel.style.display = "inline-block")) : (this.valueInput.placeholder = "请输入筛选值", 
        this.valueInputMax && (this.valueInputMax.style.display = "none"), andLabel && (andLabel.style.display = "none"));
    }
    isBooleanOperator(operator) {
        return [ "isChecked", "isUnchecked" ].includes(operator);
    }
    isRangeOperator(operator) {
        return "between" === operator || "notBetween" === operator;
    }
    applyFilter(fieldId = this.selectedField) {
        var _a, _b;
        if (!this.operatorSelect || !this.valueInput) return;
        const operator = null === (_a = this.operatorSelect) || void 0 === _a ? void 0 : _a.value;
        let conditionValue = null === (_b = this.valueInput) || void 0 === _b ? void 0 : _b.value;
        if (this.currentCategory !== types_1.FilterOperatorCategory.NUMBER || (conditionValue = parseFloat(conditionValue), 
        !isNaN(conditionValue))) {
            if (this.isRangeOperator(operator)) {
                const minValue = conditionValue;
                let maxValue = this.valueInputMax.value.trim();
                if (this.currentCategory === types_1.FilterOperatorCategory.NUMBER) {
                    const numMaxValue = parseFloat(maxValue);
                    if (isNaN(numMaxValue)) return;
                    maxValue = numMaxValue;
                }
                conditionValue = [ minValue, maxValue ];
            }
            conditionValue || !1 === conditionValue || 0 === conditionValue || this.isBooleanOperator(operator) ? (this.filterStateManager.dispatch({
                type: types_1.FilterActionType.APPLY_FILTERS,
                payload: {
                    field: fieldId,
                    type: "byCondition",
                    operator: operator,
                    condition: conditionValue,
                    enable: !0
                }
            }), this.hide()) : this.clearFilter(fieldId);
        }
    }
    clearFilter(fieldId) {
        this.filterStateManager.dispatch({
            type: types_1.FilterActionType.REMOVE_FILTER,
            payload: {
                field: fieldId
            }
        }), this.hide();
    }
    render(container) {
        const filterStyles = this.styles;
        this.filterByConditionPanel = document.createElement("div"), (0, styles_1.applyStyles)(this.filterByConditionPanel, filterStyles.filterPanel), 
        this.conditionContainer = document.createElement("div"), (0, styles_1.applyStyles)(this.conditionContainer, this.styles.conditionContainer), 
        this.categoryLabel = (0, styles_1.createElement)("label", {}, [ "筛选类型：" ]), (0, 
        styles_1.applyStyles)(this.categoryLabel, this.styles.formLabel), this.categorySelect = (0, 
        styles_1.createElement)("select"), (0, styles_1.applyStyles)(this.categorySelect, filterStyles.operatorSelect), 
        this.categories.forEach((category => {
            const option = document.createElement("option");
            option.value = category.value, option.textContent = category.label, this.categorySelect.appendChild(option);
        })), this.operatorLabel = (0, styles_1.createElement)("label", {}, [ "筛选条件：" ]), 
        (0, styles_1.applyStyles)(this.operatorLabel, this.styles.formLabel), this.operatorSelect = (0, 
        styles_1.createElement)("select"), (0, styles_1.applyStyles)(this.operatorSelect, filterStyles.operatorSelect), 
        this.valueLabel = (0, styles_1.createElement)("label", {}, [ "筛选值：" ]), (0, styles_1.applyStyles)(this.valueLabel, this.styles.formLabel), 
        this.rangeInputContainer = (0, styles_1.createElement)("div"), (0, styles_1.applyStyles)(this.rangeInputContainer, this.styles.rangeInputContainer), 
        this.valueInput = (0, styles_1.createElement)("input", {
            type: "text",
            placeholder: "请输入筛选值"
        }), (0, styles_1.applyStyles)(this.valueInput, filterStyles.searchInput), this.andLabel = (0, 
        styles_1.createElement)("span", {}, [ "和" ]), (0, styles_1.applyStyles)(this.andLabel, this.styles.addLabel), 
        this.andLabel.style.display = "none", this.valueInputMax = (0, styles_1.createElement)("input", {
            type: "text",
            placeholder: "最大值"
        }), (0, styles_1.applyStyles)(this.valueInputMax, filterStyles.searchInput), this.valueInputMax.style.display = "none", 
        this.rangeInputContainer.appendChild(this.valueInput), this.rangeInputContainer.appendChild(this.andLabel), 
        this.rangeInputContainer.appendChild(this.valueInputMax), this.conditionContainer.appendChild(this.categoryLabel), 
        this.conditionContainer.appendChild(this.categorySelect), this.conditionContainer.appendChild(this.operatorLabel), 
        this.conditionContainer.appendChild(this.operatorSelect), this.conditionContainer.appendChild(this.valueLabel), 
        this.conditionContainer.appendChild(this.rangeInputContainer), this.filterByConditionPanel.appendChild(this.conditionContainer), 
        container.appendChild(this.filterByConditionPanel), this.hide(), this.updateOperatorOptions(), 
        this.bindEvents();
    }
    updateStyles(styles) {
        (0, styles_1.applyStyles)(this.filterByConditionPanel, styles.filterPanel), (0, 
        styles_1.applyStyles)(this.conditionContainer, styles.conditionContainer), (0, styles_1.applyStyles)(this.categoryLabel, styles.formLabel), 
        (0, styles_1.applyStyles)(this.categorySelect, styles.operatorSelect), (0, styles_1.applyStyles)(this.operatorLabel, styles.formLabel), 
        (0, styles_1.applyStyles)(this.operatorSelect, styles.operatorSelect), (0, styles_1.applyStyles)(this.valueLabel, styles.formLabel), 
        (0, styles_1.applyStyles)(this.rangeInputContainer, styles.rangeInputContainer), 
        (0, styles_1.applyStyles)(this.valueInput, styles.searchInput), (0, styles_1.applyStyles)(this.andLabel, styles.addLabel), 
        (0, styles_1.applyStyles)(this.valueInputMax, styles.searchInput);
    }
    bindEvents() {
        this.valueInput.addEventListener("keypress", (event => {
            "Enter" === event.key && this.applyFilter();
        })), this.valueInputMax.addEventListener("keypress", (event => {
            "Enter" === event.key && this.applyFilter();
        })), this.categorySelect.addEventListener("change", (() => {
            this.handleCategoryChange();
        })), this.operatorSelect.addEventListener("change", (() => {
            const selectedOperator = this.operatorSelect.value, andLabel = this.valueInput.nextElementSibling;
            this.isRangeOperator(selectedOperator) ? (this.valueInput.placeholder = "最小值", this.valueInputMax.style.display = "inline-block", 
            andLabel.style.display = "inline-block") : (this.valueInput.placeholder = "请输入筛选值", 
            this.valueInputMax.style.display = "none", andLabel.style.display = "none");
        }));
    }
    show() {
        this.filterByConditionPanel && (this.filterByConditionPanel.style.display = "block", 
        this.loadCurrentFilterState());
    }
    hide() {
        this.filterByConditionPanel && (this.filterByConditionPanel.style.display = "none");
    }
}

exports.ConditionFilter = ConditionFilter;
//# sourceMappingURL=condition-filter.js.map