import { applyStyles, filterStyles, createElement } from "./styles";

import { FilterActionType, FilterOperatorCategory } from "./types";

export class ConditionFilter {
    constructor(table, filterStateManager) {
        this.currentCategory = FilterOperatorCategory.ALL, this.operators = [ {
            value: "equals",
            label: "等于",
            category: FilterOperatorCategory.ALL
        }, {
            value: "notEquals",
            label: "不等于",
            category: FilterOperatorCategory.ALL
        }, {
            value: "equals",
            label: "等于",
            category: FilterOperatorCategory.NUMBER
        }, {
            value: "notEquals",
            label: "不等于",
            category: FilterOperatorCategory.NUMBER
        }, {
            value: "greaterThan",
            label: "大于",
            category: FilterOperatorCategory.NUMBER
        }, {
            value: "lessThan",
            label: "小于",
            category: FilterOperatorCategory.NUMBER
        }, {
            value: "greaterThanOrEqual",
            label: "大于等于",
            category: FilterOperatorCategory.NUMBER
        }, {
            value: "lessThanOrEqual",
            label: "小于等于",
            category: FilterOperatorCategory.NUMBER
        }, {
            value: "between",
            label: "介于",
            category: FilterOperatorCategory.NUMBER
        }, {
            value: "notBetween",
            label: "不介于",
            category: FilterOperatorCategory.NUMBER
        }, {
            value: "equals",
            label: "等于",
            category: FilterOperatorCategory.TEXT
        }, {
            value: "notEquals",
            label: "不等于",
            category: FilterOperatorCategory.TEXT
        }, {
            value: "contains",
            label: "包含",
            category: FilterOperatorCategory.TEXT
        }, {
            value: "notContains",
            label: "不包含",
            category: FilterOperatorCategory.TEXT
        }, {
            value: "startsWith",
            label: "开头是",
            category: FilterOperatorCategory.TEXT
        }, {
            value: "notStartsWith",
            label: "开头不是",
            category: FilterOperatorCategory.TEXT
        }, {
            value: "endsWith",
            label: "结尾是",
            category: FilterOperatorCategory.TEXT
        }, {
            value: "notEndsWith",
            label: "结尾不是",
            category: FilterOperatorCategory.TEXT
        }, {
            value: "equals",
            label: "等于",
            category: FilterOperatorCategory.COLOR
        }, {
            value: "notEquals",
            label: "不等于",
            category: FilterOperatorCategory.COLOR
        }, {
            value: "isChecked",
            label: "已选中",
            category: FilterOperatorCategory.CHECKBOX
        }, {
            value: "isUnchecked",
            label: "未选中",
            category: FilterOperatorCategory.CHECKBOX
        }, {
            value: "isChecked",
            label: "已选中",
            category: FilterOperatorCategory.RADIO
        }, {
            value: "isUnchecked",
            label: "未选中",
            category: FilterOperatorCategory.RADIO
        } ], this.categories = [ {
            value: FilterOperatorCategory.ALL,
            label: "全部"
        }, {
            value: FilterOperatorCategory.TEXT,
            label: "文本"
        }, {
            value: FilterOperatorCategory.NUMBER,
            label: "数值"
        }, {
            value: FilterOperatorCategory.COLOR,
            label: "颜色"
        }, {
            value: FilterOperatorCategory.CHECKBOX,
            label: "复选框"
        }, {
            value: FilterOperatorCategory.RADIO,
            label: "单选框"
        } ], this.table = table, this.filterStateManager = filterStateManager;
    }
    setSelectedField(fieldId) {
        this.selectedField = fieldId, this.updateOperatorOptions(), this.loadCurrentFilterState();
    }
    updateOperatorOptions() {
        if (!this.operatorSelect) return;
        let filteredOperators;
        if (this.operatorSelect.innerHTML = "", this.currentCategory === FilterOperatorCategory.ALL) {
            const uniqueOperators = new Map;
            this.operators.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 = FilterOperatorCategory.ALL, this.categorySelect && (this.categorySelect.value = 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 !== FilterOperatorCategory.NUMBER || (conditionValue = parseFloat(conditionValue), 
        !isNaN(conditionValue))) {
            if (this.isRangeOperator(operator)) {
                const minValue = conditionValue;
                let maxValue = this.valueInputMax.value.trim();
                if (this.currentCategory === 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: 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: FilterActionType.REMOVE_FILTER,
            payload: {
                field: fieldId
            }
        }), this.hide();
    }
    render(container) {
        this.filterByConditionPanel = document.createElement("div"), applyStyles(this.filterByConditionPanel, filterStyles.filterPanel);
        const conditionContainer = document.createElement("div");
        applyStyles(conditionContainer, filterStyles.conditionContainer);
        const categoryLabel = createElement("label", {}, [ "筛选类型：" ]);
        applyStyles(categoryLabel, filterStyles.formLabel), this.categorySelect = createElement("select"), 
        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);
        }));
        const operatorLabel = createElement("label", {}, [ "筛选条件：" ]);
        applyStyles(operatorLabel, filterStyles.formLabel), this.operatorSelect = createElement("select"), 
        applyStyles(this.operatorSelect, filterStyles.operatorSelect);
        const valueLabel = createElement("label", {}, [ "筛选值：" ]);
        applyStyles(valueLabel, filterStyles.formLabel);
        const rangeInputContainer = createElement("div");
        applyStyles(rangeInputContainer, filterStyles.rangeInputContainer), this.valueInput = createElement("input", {
            type: "text",
            placeholder: "请输入筛选值"
        }), applyStyles(this.valueInput, filterStyles.searchInput);
        const andLabel = createElement("span", {}, [ "和" ]);
        applyStyles(andLabel, filterStyles.addLabel), andLabel.style.display = "none", this.valueInputMax = createElement("input", {
            type: "text",
            placeholder: "最大值"
        }), applyStyles(this.valueInputMax, filterStyles.searchInput), this.valueInputMax.style.display = "none", 
        rangeInputContainer.appendChild(this.valueInput), rangeInputContainer.appendChild(andLabel), 
        rangeInputContainer.appendChild(this.valueInputMax), conditionContainer.appendChild(categoryLabel), 
        conditionContainer.appendChild(this.categorySelect), conditionContainer.appendChild(operatorLabel), 
        conditionContainer.appendChild(this.operatorSelect), conditionContainer.appendChild(valueLabel), 
        conditionContainer.appendChild(rangeInputContainer), this.filterByConditionPanel.appendChild(conditionContainer), 
        container.appendChild(this.filterByConditionPanel), this.hide(), this.updateOperatorOptions(), 
        this.bindEvents();
    }
    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");
    }
}
//# sourceMappingURL=condition-filter.js.map