"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Filter", {
    enumerable: true,
    get: function() {
        return Filter;
    }
});
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _hooks = require("../../../../../../hooks");
const _Icon = require("../../../../../_Icon");
const _HeadCell = require("../../HeadCell");
const _Tablestyles = require("../../../../Table.styles");
const _Tabletokens = require("../../../../Table.tokens");
const _Filterstyles = require("./Filter.styles");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const getSelectWidth = (size)=>{
    switch(size){
        case 'l':
            {
                return '18.75rem';
            }
        case 'm':
            {
                return '16rem';
            }
        case 's':
            {
                return '14rem';
            }
        default:
            {
                return '14rem';
            }
    }
};
const isFilterChanged = (outerFiltered, localFiltered)=>{
    return outerFiltered.length !== localFiltered.length || new Set([
        ...outerFiltered,
        ...localFiltered
    ]).size !== outerFiltered.length;
};
const Filter = ({ header, size, outerFiltered, tableContainerRef })=>{
    const filtered = header.column.getFilterValue() || [];
    const [localFiltered, setLocalFiltered] = (0, _react.useState)(filtered || []);
    const { filters } = header?.column?.columnDef?.meta || {};
    const isDisabled = !isFilterChanged(filtered, localFiltered);
    const handleFilterSubmit = ()=>{
        header.column.setFilterValue(localFiltered);
    };
    const handleReset = ()=>{
        setLocalFiltered([]);
    };
    const handleToggle = (opened)=>{
        if (!opened) {
            setLocalFiltered(header.column.getFilterValue() || []);
        }
    };
    (0, _hooks.useDidMountEffect)(()=>{
        setLocalFiltered(filtered);
    }, [
        outerFiltered
    ]);
    return /*#__PURE__*/ _react.default.createElement(_Filterstyles.Select, {
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        placement: "bottom-end",
        multiselect: true,
        value: localFiltered,
        onChange: setLocalFiltered,
        renderTarget: ()=>/*#__PURE__*/ _react.default.createElement(_Filterstyles.IconFilterWrapper, null, /*#__PURE__*/ _react.default.createElement(_Icon.IconFilterFunnel, {
                size: (0, _HeadCell.getIconSize)(size),
                className: _Tabletokens.classes.filterIcon
            }), filtered.length ? /*#__PURE__*/ _react.default.createElement(_Filterstyles.StyledIndicator, {
                size: "s",
                view: "accent"
            }) : undefined),
        items: filters || [],
        portal: tableContainerRef,
        listWidth: getSelectWidth(size),
        onToggle: handleToggle,
        afterList: /*#__PURE__*/ _react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/ _react.default.createElement(_Filterstyles.StyledDivider, {
            orientation: "horizontal",
            length: "auto"
        }), /*#__PURE__*/ _react.default.createElement(_Filterstyles.ControlPanel, null, /*#__PURE__*/ _react.default.createElement(_Tablestyles.LinkButton, {
            onClick: handleReset
        }, "Сбросить фильтр"), /*#__PURE__*/ _react.default.createElement(_Tablestyles.ButtonAccent, {
            disabled: isDisabled,
            onClick: handleFilterSubmit
        }, "Применить")))
    });
};
