"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FiltersSelected = void 0;
const react_1 = __importDefault(require("react"));
const SelectedFilters_1 = __importDefault(require("@usereactify/search-internals/lib/components/basic/SelectedFilters"));
const components_1 = require("../../components");
const hooks_1 = require("../../hooks");
const FiltersSelected = (props) => {
    var _a;
    const context = (0, hooks_1.useReactifySearchContext)();
    const filtersHook = (0, hooks_1.useFilters)();
    const RenderComponent = (_a = props.render) !== null && _a !== void 0 ? _a : components_1.ExampleFiltersSelected;
    return (react_1.default.createElement(SelectedFilters_1.default, { render: ({ selectedValues, setValue }) => {
            const selectedFilters = Object.entries(selectedValues)
                .filter(([key, item]) => {
                var _a, _b;
                return (item === null || item === void 0 ? void 0 : item.URLParams) &&
                    (item === null || item === void 0 ? void 0 : item.showFilter) &&
                    (!!((_a = item === null || item === void 0 ? void 0 : item.value) === null || _a === void 0 ? void 0 : _a.length) || !!((_b = item === null || item === void 0 ? void 0 : item.value) === null || _b === void 0 ? void 0 : _b.label));
            })
                .map(([key, item]) => ({
                key: key,
                label: item.label,
                value: item.value,
            }));
            const handleRemove = (selectedFilterKey, selectedFilterValues = []) => {
                var _a;
                const filter = (_a = filtersHook.filters) === null || _a === void 0 ? void 0 : _a.find((filter) => filter.handle === selectedFilterKey);
                if (!filter) {
                    return;
                }
                if (filter.displayType === "slider") {
                    const event = new CustomEvent(`@usereactify/search:${context.options.mode}:filter:${filter.handle}:update`, {
                        detail: {
                            handle: filter.handle,
                            value: [],
                        },
                    });
                    window.dispatchEvent(event);
                    return;
                }
                if (selectedFilterValues.length === 0) {
                    setValue(selectedFilterKey, null);
                    return;
                }
                if (selectedFilterValues.length > 0) {
                    if (filter.displayType === "single") {
                        setValue(selectedFilterKey, null);
                    }
                    if (filter.displayType === "multi") {
                        if (filter.displayView === "range") {
                            const values = selectedValues[selectedFilterKey].value
                                .filter((value) => !selectedFilterValues.includes(value.label))
                                .map((value) => value.label);
                            const event = new CustomEvent(`@usereactify/search:${context.options.mode}:filter:${filter.handle}:update`, {
                                detail: {
                                    handle: filter.handle,
                                    value: values,
                                },
                            });
                            window.dispatchEvent(event);
                        }
                        if (filter.displayView !== "range") {
                            const values = selectedValues[selectedFilterKey].value.filter((value) => !selectedFilterValues.includes(value));
                            setValue(selectedFilterKey, values);
                        }
                    }
                }
            };
            if (!selectedFilters.length) {
                return null;
            }
            return (react_1.default.createElement(RenderComponent, { selectedFilters: selectedFilters, handleRemove: handleRemove }));
        } }));
};
exports.FiltersSelected = FiltersSelected;
//# sourceMappingURL=FiltersSelected.js.map