"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useFilterRangeProps = void 0;
const react_1 = __importDefault(require("react"));
const hooks_1 = require("../hooks");
const utility_1 = require("../utility");
const useFilterRangeProps = (filter) => {
    const context = (0, hooks_1.useReactifySearchContext)();
    const { track } = (0, hooks_1.useAnalytics)();
    const [filterValues, setFilterValues] = react_1.default.useState([]);
    const handleChange = react_1.default.useCallback((key) => {
        utility_1.debug.log("useFilterRangeMultiProps", "handleChange[key]", key);
        if (filter.displayType === "multi") {
            const selected = filterValues.find((item) => item === key)
                ? filterValues.filter((value) => value !== key)
                : [...filterValues, key];
            setFilterValues(selected);
        }
        if (filter.displayType === "single") {
            setFilterValues([key]);
        }
        track({
            eventName: "filterChange",
            payload: {
                name: filter.name,
                value: key,
            },
        });
    }, [filter, filterValues]);
    const handleClear = react_1.default.useCallback(() => {
        setFilterValues([]);
    }, []);
    react_1.default.useEffect(() => {
        var _a;
        try {
            if (filter.displayView === "range") {
                const url = new URL(window.location.href);
                const value = JSON.parse((_a = url.searchParams.get(filter.handle)) !== null && _a !== void 0 ? _a : "[]");
                setFilterValues(value);
            }
        }
        catch (error) {
            console.error(error);
        }
    }, []);
    react_1.default.useEffect(() => {
        const handler = (event) => {
            setFilterValues(event.detail.value);
        };
        window.addEventListener(`@usereactify/search:${context.options.mode}:filter:${filter.handle}:update`, handler);
        return () => window.removeEventListener(`@usereactify/search:${context.options.mode}:filter:${filter.handle}:update`, handler);
    }, [context.options.mode]);
    const filterRangeProps = react_1.default.useMemo(() => {
        var _a, _b;
        const options = (_b = (_a = filter.displayRangeOptions) === null || _a === void 0 ? void 0 : _a.map((option) => {
            const [key] = option.split(":");
            return {
                key: key,
                label: filter.settingsUppercase ? key.toUpperCase() : key,
                checked: filterValues.includes(key),
            };
        })) !== null && _b !== void 0 ? _b : [];
        return {
            handleChange,
            handleClear,
            filter,
            values: filterValues,
            options,
        };
    }, [filter, filterValues, handleChange]);
    return filterRangeProps;
};
exports.useFilterRangeProps = useFilterRangeProps;
//# sourceMappingURL=useFilterRangeProps.js.map