"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Filter", {
    enumerable: true,
    get: function() {
        return Filter;
    }
});
var _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
var _hooks = require("../../../../../../hooks");
var _Icon = require("../../../../../_Icon");
var _HeadCell = require("../../HeadCell");
var _Tablestyles = require("../../../../Table.styles");
var _Tabletokens = require("../../../../Table.tokens");
var _Filterstyles = require("./Filter.styles");
function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
function _array_without_holes(arr) {
    if (Array.isArray(arr)) return _array_like_to_array(arr);
}
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;
}
function _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _non_iterable_spread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _to_consumable_array(arr) {
    return _array_without_holes(arr) || _iterable_to_array(arr) || _unsupported_iterable_to_array(arr) || _non_iterable_spread();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
var getSelectWidth = function getSelectWidth(size) {
    switch(size){
        case 'l':
            {
                return '18.75rem';
            }
        case 'm':
            {
                return '16rem';
            }
        case 's':
            {
                return '14rem';
            }
        default:
            {
                return '14rem';
            }
    }
};
var isFilterChanged = function isFilterChanged(outerFiltered, localFiltered) {
    return outerFiltered.length !== localFiltered.length || new Set(_to_consumable_array(outerFiltered).concat(_to_consumable_array(localFiltered))).size !== outerFiltered.length;
};
var Filter = function Filter(param) {
    var header = param.header, size = param.size, outerFiltered = param.outerFiltered, tableContainerRef = param.tableContainerRef;
    var _header_column_columnDef, _header_column;
    var filtered = header.column.getFilterValue() || [];
    var _useState = _sliced_to_array((0, _react.useState)(filtered || []), 2), localFiltered = _useState[0], setLocalFiltered = _useState[1];
    var filters = ((header === null || header === void 0 ? void 0 : (_header_column = header.column) === null || _header_column === void 0 ? void 0 : (_header_column_columnDef = _header_column.columnDef) === null || _header_column_columnDef === void 0 ? void 0 : _header_column_columnDef.meta) || {}).filters;
    var isDisabled = !isFilterChanged(filtered, localFiltered);
    var handleFilterSubmit = function handleFilterSubmit() {
        header.column.setFilterValue(localFiltered);
    };
    var handleReset = function handleReset() {
        setLocalFiltered([]);
    };
    var handleToggle = function handleToggle(opened) {
        if (!opened) {
            setLocalFiltered(header.column.getFilterValue() || []);
        }
    };
    (0, _hooks.useDidMountEffect)(function() {
        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: function renderTarget() {
            return /*#__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
        }, "Применить")))
    });
};
