"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Filter = void 0;
const react_1 = __importDefault(require("react"));
const ReactiveComponent_1 = __importDefault(require("@usereactify/search-internals/lib/components/basic/ReactiveComponent"));
const MultiList_1 = __importDefault(require("@usereactify/search-internals/lib/components/list/MultiList"));
const SingleList_1 = __importDefault(require("@usereactify/search-internals/lib/components/list/SingleList"));
const SingleRange_1 = __importDefault(require("@usereactify/search-internals/lib/components/range/SingleRange"));
const MultiRange_1 = __importDefault(require("@usereactify/search-internals/lib/components/range/MultiRange"));
const hooks_1 = require("../../hooks");
const components_1 = require("../../components");
const props_1 = require("../../utility/props");
const useReactSliderProps_1 = require("../../hooks/react-slider/useReactSliderProps");
const Filter = (props) => {
    if ("single" === props.filter.displayType) {
        if ("range" === props.filter.displayView) {
            return react_1.default.createElement(FilterRangeSingle, Object.assign({}, props));
        }
        return react_1.default.createElement(FilterSingleList, Object.assign({}, props));
    }
    if ("multi" === props.filter.displayType) {
        if ("range" === props.filter.displayView) {
            return react_1.default.createElement(FilterRangeMulti, Object.assign({}, props));
        }
        return react_1.default.createElement(FilterMultiList, Object.assign({}, props));
    }
    if ("slider" === props.filter.displayType) {
        return react_1.default.createElement(FilterSlider, Object.assign({}, props));
    }
    // TODO: Legacy range filter, migrate to new single and multi ranges
    if ("range" === props.filter.displayType) {
        return react_1.default.createElement(FilterRangeSingle, Object.assign({}, props));
    }
    console.warn(`filter with display type "${props.filter.displayType}" not yet supported`);
    return null;
};
exports.Filter = Filter;
const FilterSingleList = (props) => {
    const reactifySearchContext = (0, hooks_1.useReactifySearchContext)();
    const filtersHook = (0, hooks_1.useFilters)();
    const sharedProps = (0, props_1.getPropsShared)({
        provider: reactifySearchContext.options,
        config: reactifySearchContext.config,
        filterGroup: filtersHook.filterStack,
    }, props.filter);
    const reactiveProps = (0, props_1.getPropsFilterList)(props.filter);
    return (react_1.default.createElement(SingleList_1.default, Object.assign({}, sharedProps, reactiveProps, { render: (reactivesearchFilterProps) => (react_1.default.createElement(FilterListInner, Object.assign({}, props, { reactivesearchFilterProps: reactivesearchFilterProps }))) })));
};
const FilterMultiList = (props) => {
    const reactifySearchContext = (0, hooks_1.useReactifySearchContext)();
    const filtersHook = (0, hooks_1.useFilters)();
    const sharedProps = (0, props_1.getPropsShared)({
        provider: reactifySearchContext.options,
        config: reactifySearchContext.config,
        filterGroup: filtersHook.filterStack,
    }, props.filter);
    const reactiveProps = (0, props_1.getPropsFilterList)(props.filter);
    return (react_1.default.createElement(MultiList_1.default, Object.assign({}, sharedProps, reactiveProps, { render: (reactivesearchFilterProps) => (react_1.default.createElement(FilterListInner, Object.assign({}, props, { reactivesearchFilterProps: reactivesearchFilterProps }))) })));
};
const FilterRangeSingle = (props) => {
    var _a;
    const reactifySearchContext = (0, hooks_1.useReactifySearchContext)();
    const filtersHook = (0, hooks_1.useFilters)();
    const sharedProps = (0, props_1.getPropsShared)({
        provider: reactifySearchContext.options,
        config: reactifySearchContext.config,
        filterGroup: filtersHook.filterStack,
    }, props.filter);
    const reactiveProps = (0, props_1.getPropsFilterRange)(props.filter);
    const filterRangeProps = (0, hooks_1.useFilterRangeProps)(props.filter);
    const RenderRangeComponent = (_a = props.renderFilterRange) !== null && _a !== void 0 ? _a : components_1.ExampleFilterRange;
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("div", { style: {
                display: "none",
            } },
            react_1.default.createElement(SingleRange_1.default, Object.assign({}, sharedProps, reactiveProps, { value: filterRangeProps.values[0], onChange: filterRangeProps.handleChange }))),
        react_1.default.createElement(RenderRangeComponent, { filterRangeProps: filterRangeProps, filter: props.filter })));
};
const FilterRangeMulti = (props) => {
    var _a;
    const reactifySearchContext = (0, hooks_1.useReactifySearchContext)();
    const filtersHook = (0, hooks_1.useFilters)();
    const sharedProps = (0, props_1.getPropsShared)({
        provider: reactifySearchContext.options,
        config: reactifySearchContext.config,
        filterGroup: filtersHook.filterStack,
    }, props.filter);
    const reactiveProps = (0, props_1.getPropsFilterRange)(props.filter);
    const filterRangeProps = (0, hooks_1.useFilterRangeProps)(props.filter);
    const RenderRangeComponent = (_a = props.renderFilterRange) !== null && _a !== void 0 ? _a : components_1.ExampleFilterRange;
    return (react_1.default.createElement(react_1.default.Fragment, null,
        react_1.default.createElement("div", { style: {
                display: "none",
            } },
            react_1.default.createElement(MultiRange_1.default, Object.assign({}, sharedProps, reactiveProps, { value: filterRangeProps.values, 
                // @note: onChange handler must be defined for controlled component to work correctly
                onChange: () => { }, onValueChange: (values) => {
                    if (values.length === 0) {
                        filterRangeProps.handleClear();
                    }
                } }))),
        react_1.default.createElement(RenderRangeComponent, { filterRangeProps: filterRangeProps, filter: props.filter })));
};
const FilterSlider = (props) => {
    const reactifySearchContext = (0, hooks_1.useReactifySearchContext)();
    const filtersHook = (0, hooks_1.useFilters)();
    const sharedProps = (0, props_1.getPropsShared)({
        provider: reactifySearchContext.options,
        config: reactifySearchContext.config,
        filterGroup: filtersHook.filterStack,
    }, props.filter);
    const filterSliderProps = (0, hooks_1.useFilterSliderProps)(props.filter);
    const reactiveProps = (0, props_1.getPropsFilterSlider)(props.filter, "", filterSliderProps.value);
    return (react_1.default.createElement(ReactiveComponent_1.default, Object.assign({}, sharedProps, { defaultQuery: () => reactiveProps.defaultQuery, value: filterSliderProps.value, render: (reactivesearchFilterProps) => {
            return (react_1.default.createElement(FilterSliderInner, Object.assign({}, props, { filterSliderProps: filterSliderProps, customQuery: reactiveProps.customQuery, reactivesearchFilterProps: reactivesearchFilterProps })));
        } })));
};
// inner component exists only so we can use memos in reactivesearch render functions
const FilterSliderInner = (props) => {
    var _a, _b, _c, _d, _e, _f, _g;
    const { filter, renderFilterSlider, filterSliderProps, reactivesearchFilterProps } = props, otherProps = __rest(props, ["filter", "renderFilterSlider", "filterSliderProps", "reactivesearchFilterProps"]);
    const filterRange = [
        (_c = (_b = (_a = reactivesearchFilterProps.aggregations) === null || _a === void 0 ? void 0 : _a.min) === null || _b === void 0 ? void 0 : _b.value) !== null && _c !== void 0 ? _c : 0,
        (_f = (_e = (_d = reactivesearchFilterProps.aggregations) === null || _d === void 0 ? void 0 : _d.max) === null || _e === void 0 ? void 0 : _e.value) !== null && _f !== void 0 ? _f : 0,
    ];
    const reactSliderProps = (0, useReactSliderProps_1.useReactSliderProps)(filter, filterSliderProps.value, filterRange, filterSliderProps.handleChange);
    react_1.default.useEffect(() => {
        if (!props.customQuery) {
            return;
        }
        props.reactivesearchFilterProps.setQuery({
            query: props.customQuery,
            value: filterSliderProps.value,
        });
    }, [filterSliderProps.value]);
    const RenderSliderComponent = (_g = props.renderFilterSlider) !== null && _g !== void 0 ? _g : components_1.ExampleFilterSlider;
    return (react_1.default.createElement(RenderSliderComponent, Object.assign({ filterSliderProps: filterSliderProps, reactSliderProps: reactSliderProps, filter: filter }, otherProps)));
};
// inner component exists only so we can use memos in reactivesearch render functions
const FilterListInner = (props) => {
    var _a;
    const { filter, renderFilterList, reactivesearchFilterProps } = props, otherProps = __rest(props, ["filter", "renderFilterList", "reactivesearchFilterProps"]);
    const filterListProps = (0, hooks_1.useFilterListProps)(filter, reactivesearchFilterProps);
    // hide entire filter when no options available
    if (!props.showEmptyFilters && 0 === filterListProps.options.length) {
        return null;
    }
    const RenderListComponent = (_a = props.renderFilterList) !== null && _a !== void 0 ? _a : components_1.ExampleFilterList;
    return (react_1.default.createElement(RenderListComponent, Object.assign({ filterListProps: filterListProps, filter: filter }, otherProps)));
};
//# sourceMappingURL=Filter.js.map