"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ClearAll = 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 ClearAll = (props) => {
    var _a;
    const RenderComponent = (_a = props.render) !== null && _a !== void 0 ? _a : components_1.ExampleClearAll;
    const filtersHook = (0, hooks_1.useFilters)();
    return (react_1.default.createElement(SelectedFilters_1.default, { render: (renderProps) => {
            const selectedFilters = Object.values(renderProps.selectedValues)
                .map((value) => value)
                .filter((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));
            });
            const handleClearAll = () => {
                var _a;
                for (const filter of (_a = filtersHook.filters) !== null && _a !== void 0 ? _a : []) {
                    if (filter.displayType === "slider") {
                        const event = new CustomEvent(`@usereactify/search:filter:${filter.handle}:update`, {
                            detail: {
                                handle: filter.handle,
                                value: [],
                            },
                        });
                        window.dispatchEvent(event);
                    }
                    if (filter.displayType === "multi") {
                        if (filter.displayView === "range") {
                            const event = new CustomEvent(`@usereactify/search:filter:${filter.handle}:update`, {
                                detail: {
                                    handle: filter.handle,
                                    value: [],
                                },
                            });
                            window.dispatchEvent(event);
                        }
                    }
                }
                renderProps.clearValues();
            };
            if (props.showWhenEmpty !== true && !selectedFilters.length) {
                return null;
            }
            return react_1.default.createElement(RenderComponent, { clearAll: handleClearAll });
        } }));
};
exports.ClearAll = ClearAll;
//# sourceMappingURL=ClearAll.js.map