"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useFilterSliderProps = void 0;
const react_1 = __importDefault(require("react"));
const hooks_1 = require("../hooks");
const utility_1 = require("../utility");
const useFilterSliderProps = (filter) => {
    var _a;
    const context = (0, hooks_1.useReactifySearchContext)();
    const { track } = (0, hooks_1.useAnalytics)();
    const urlSearchParam = new URLSearchParams(typeof window === "undefined" ? context.options.query : window.location.search);
    const [filterValue, setFilterValue] = react_1.default.useState(JSON.parse((_a = urlSearchParam.get(filter.handle)) !== null && _a !== void 0 ? _a : "[0,0]"));
    const handleChange = react_1.default.useCallback((value) => {
        const valueFormatted = [parseInt(parseInt(`${value[0]}`).toFixed(0)), parseInt(parseInt(`${value[1]}`).toFixed(0))];
        utility_1.debug.log("useFilterSliderProps", "handleChange[valueFormatted]", valueFormatted);
        setFilterValue(valueFormatted);
        track({
            eventName: "filterChange",
            payload: {
                name: filter.name,
                value: `${valueFormatted[0]}:${valueFormatted[1]}`,
            },
        });
    }, [filter]);
    react_1.default.useEffect(() => {
        const handler = (event) => {
            var _a;
            const valueFormatted = ((_a = event.detail.value) === null || _a === void 0 ? void 0 : _a.length) > 0 ? [parseInt(event.detail.value[0].toFixed(0)), parseInt(event.detail.value[0].toFixed(0))] : [];
            setFilterValue(valueFormatted);
        };
        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 filterSliderProps = react_1.default.useMemo(() => {
        return {
            handleChange,
            filter,
            value: filterValue,
        };
    }, [
        handleChange,
        filter,
        filterValue,
    ]);
    return filterSliderProps;
};
exports.useFilterSliderProps = useFilterSliderProps;
//# sourceMappingURL=useFilterSliderProps.js.map