"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 useReactSliderProps_1 = require("./react-slider/useReactSliderProps");
const useFilterSliderProps = (filter) => {
    var _a;
    const { track } = (0, hooks_1.useAnalytics)();
    const urlSearchParam = new URLSearchParams(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 [filterRange, setFilterRange] = react_1.default.useState([
        0, 0,
    ]);
    const handleChange = react_1.default.useCallback((value) => {
        utility_1.debug.log("useFilterSliderProps", "handleChange[value]", value);
        setFilterValue([parseInt(`${value[0]}`), parseInt(`${value[1]}`)]);
        track({
            eventName: "filterChange",
            payload: {
                name: filter.name,
                value: `${value[0]}:${value[1]}`,
            },
        });
    }, [filter]);
    const handleRange = react_1.default.useCallback((value) => {
        setFilterRange([parseInt(`${value[0]}`), parseInt(`${value[1]}`)]);
    }, [filter]);
    react_1.default.useEffect(() => {
        const handler = (event) => {
            setFilterValue(event.detail.value);
        };
        window.addEventListener(`@usereactify/search:filter:${filter.handle}:update`, handler);
        return () => window.removeEventListener(`@usereactify/search:filter:${filter.handle}:update`, handler);
    }, []);
    const reactSliderProps = (0, useReactSliderProps_1.useReactSliderProps)(filter, filterValue, filterRange, handleChange);
    const filterSliderProps = react_1.default.useMemo(() => {
        return {
            handleChange,
            handleRange,
            filter,
            value: filterValue,
            range: filterRange,
            reactSliderProps,
        };
    }, [
        handleChange,
        handleRange,
        filter,
        filterValue,
        filterRange,
        reactSliderProps,
    ]);
    return filterSliderProps;
};
exports.useFilterSliderProps = useFilterSliderProps;
//# sourceMappingURL=useFilterSliderProps.js.map