"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useFilterListProps = void 0;
const react_1 = __importDefault(require("react"));
const hooks_1 = require("../hooks");
const utility_1 = require("../utility");
const DEFAULT_NUMERIC_SORT = ["0000", "000", "00", "0"];
const DEFAULT_ALPHABETICAL_SORT = [
    "SINGLE",
    "DOUBLE",
    "QUEEN",
    "KING",
    "SUPER KING",
    "XXXS",
    "XXS",
    "XS",
    "XS/S",
    "S",
    "SM",
    "S/M",
    "M",
    "ML",
    "M/L",
    "L",
    "LXL",
    "L/XL",
    "XL",
    "2L",
    "XXL",
    "3L",
    "XXXL",
    "4L",
    "5L",
    "6L",
    "OS",
    "ONESIZE",
];
// converts reactivesearch props returned in filters to a cleaned up/filtered version
const useFilterListProps = (filter, reactivesearchFilterProps) => {
    var _a;
    const { track } = (0, hooks_1.useAnalytics)();
    const handleChange = react_1.default.useCallback((key) => {
        utility_1.debug.log("useFilterListProps", "handleChange[key]", key);
        reactivesearchFilterProps.handleChange(key);
        track({
            eventName: "filterChange",
            payload: {
                name: filter.name,
                value: key,
            },
        });
    }, [filter]);
    const customSortOrder = (_a = filter.customSortOrder) === null || _a === void 0 ? void 0 : _a.split(",").map((item) => item.trim().toUpperCase());
    const filterListProps = react_1.default.useMemo(() => {
        var _a, _b, _c;
        let data = reactivesearchFilterProps.data;
        if ((_c = (_b = (_a = reactivesearchFilterProps === null || reactivesearchFilterProps === void 0 ? void 0 : reactivesearchFilterProps.rawData) === null || _a === void 0 ? void 0 : _a.aggregations) === null || _b === void 0 ? void 0 : _b.reactivesearch_nested) === null || _c === void 0 ? void 0 : _c.reactify_nested_outer) {
            const nestedOptions = (0, hooks_1.replaceDocCountWithParent)(reactivesearchFilterProps.rawData.aggregations.reactivesearch_nested.reactify_nested_outer);
            const nestedOptionsKey = Object.keys(nestedOptions).filter(key => key !== "doc_count");
            data = nestedOptions[nestedOptionsKey[0]].buckets;
        }
        // remove excluded options, sort alphabeticaly, map checked boolean
        const options = data
            .filter(({ key }) => {
            var _a, _b;
            if ((_a = filter.valuesExclude) === null || _a === void 0 ? void 0 : _a.includes(key))
                return false;
            if (!!((_b = filter.valuesManual) === null || _b === void 0 ? void 0 : _b.length) && !filter.valuesManual.includes(key))
                return false;
            return true;
        })
            .sort((a, b) => DEFAULT_NUMERIC_SORT.indexOf(a.key) -
            DEFAULT_NUMERIC_SORT.indexOf(b.key))
            .sort((a, b) => new Intl.Collator("en", { numeric: true, sensitivity: "base" }).compare(a.key, b.key))
            .sort((a, b) => {
            if (!customSortOrder || customSortOrder.length === 0) {
                return (DEFAULT_ALPHABETICAL_SORT.indexOf(a.key.toUpperCase()) -
                    DEFAULT_ALPHABETICAL_SORT.indexOf(b.key.toUpperCase()));
            }
            return ((customSortOrder === null || customSortOrder === void 0 ? void 0 : customSortOrder.indexOf(a.key.toUpperCase())) -
                (customSortOrder === null || customSortOrder === void 0 ? void 0 : customSortOrder.indexOf(b.key.toUpperCase())));
        })
            .map((option) => (Object.assign(Object.assign({}, option), { label: filter.settingsUppercase ? option.key.toUpperCase() : option.key, checked: (typeof reactivesearchFilterProps.value === "object" &&
                reactivesearchFilterProps.value[option.key]) ||
                reactivesearchFilterProps.value === option.key })));
        // work out if at least one option selected
        const hasSelected = (typeof reactivesearchFilterProps.value === "object" &&
            0 < Object.keys(reactivesearchFilterProps.value).length) ||
            (typeof reactivesearchFilterProps.value !== "object" &&
                !!reactivesearchFilterProps.value);
        const totalSelected = options.filter((option) => option.checked).length;
        return Object.assign(Object.assign({}, reactivesearchFilterProps), { handleChange,
            filter,
            options,
            hasSelected,
            totalSelected });
    }, [filter, handleChange, reactivesearchFilterProps]);
    return filterListProps;
};
exports.useFilterListProps = useFilterListProps;
//# sourceMappingURL=useFilterListProps.js.map