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_without_holes(arr) {
    if (Array.isArray(arr)) return _array_like_to_array(arr);
}
function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _iterable_to_array(iter) {
    if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
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 _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
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);
}
import { ChevronDown } from "@sofa-design/icons";
import { Button, ConfigProvider, Dropdown, Segmented } from "antd";
import React, { useContext, useMemo, useRef } from "react";
import { I18nContext } from "../../../../I18n";
import { debounce } from "../../utils";
import { useStyle } from "./style";
var ChartFilterComponent = function(param) {
    var filterOptions = param.filterOptions, selectedFilter = param.selectedFilter, onFilterChange = param.onFilterChange, customOptions = param.customOptions, selectedCustomSelection = param.selectedCustomSelection, onSelectionChange = param.onSelectionChange, _param_className = param.className, className = _param_className === void 0 ? '' : _param_className, classNamesProp = param.classNames, style = param.style, styles = param.styles, _param_theme = param.theme, theme = _param_theme === void 0 ? 'light' : _param_theme, _param_variant = param.variant, variant = _param_variant === void 0 ? 'default' : _param_variant;
    var _Object;
    var _customOptions_find, _i18n_locale;
    var getPrefixCls = useContext(ConfigProvider.ConfigContext).getPrefixCls;
    var i18n = useContext(I18nContext);
    var prefixCls = getPrefixCls('chart-filter');
    var _useStyle = useStyle(prefixCls), wrapSSR = _useStyle.wrapSSR, hashId = _useStyle.hashId;
    // 使用 useRef 保存最新的回调函数，避免防抖函数闭包问题
    var onFilterChangeRef = useRef(onFilterChange);
    var onSelectionChangeRef = useRef(onSelectionChange);
    // 更新 ref，确保总是使用最新的回调
    React.useEffect(function() {
        onFilterChangeRef.current = onFilterChange;
        onSelectionChangeRef.current = onSelectionChange;
    }, [
        onFilterChange,
        onSelectionChange
    ]);
    // 创建防抖的回调函数，1秒更新一次
    var debouncedFilterChange = useMemo(function() {
        return debounce(function(value) {
            if (onFilterChangeRef.current) {
                onFilterChangeRef.current(value);
            }
        }, 1000);
    }, []);
    var debouncedSelectionChange = useMemo(function() {
        return debounce(function(region) {
            if (onSelectionChangeRef.current) {
                onSelectionChangeRef.current(region);
            }
        }, 1000);
    }, []);
    // 组件卸载时清理防抖函数
    React.useEffect(function() {
        return function() {
            var _debouncedFilterChange_cancel, _debouncedSelectionChange_cancel;
            debouncedFilterChange === null || debouncedFilterChange === void 0 ? void 0 : (_debouncedFilterChange_cancel = debouncedFilterChange.cancel) === null || _debouncedFilterChange_cancel === void 0 ? void 0 : _debouncedFilterChange_cancel.call(debouncedFilterChange);
            debouncedSelectionChange === null || debouncedSelectionChange === void 0 ? void 0 : (_debouncedSelectionChange_cancel = debouncedSelectionChange.cancel) === null || _debouncedSelectionChange_cancel === void 0 ? void 0 : _debouncedSelectionChange_cancel.call(debouncedSelectionChange);
        };
    }, [
        debouncedFilterChange,
        debouncedSelectionChange
    ]);
    var handleRegionChange = function(region) {
        debouncedSelectionChange(region);
    };
    var handleFilterChange = function(value) {
        debouncedFilterChange(value);
    };
    var hasMain = Array.isArray(filterOptions) && filterOptions.length > 1;
    var hasSecondary = Array.isArray(customOptions) && customOptions.length > 1;
    if (!hasMain && !hasSecondary) {
        return null;
    }
    if (!filterOptions || filterOptions.length < 2) {
        return null;
    }
    var mergedClassName = [
        prefixCls,
        "".concat(prefixCls, "-").concat(theme),
        "".concat(prefixCls, "-").concat(variant),
        hashId,
        className,
        classNamesProp
    ].filter(Boolean).join(' ');
    var mergedStyle = _object_spread({}, style, Array.isArray(styles) ? (_Object = Object).assign.apply(_Object, [
        {}
    ].concat(_to_consumable_array(styles))) : styles || {});
    return wrapSSR(/*#__PURE__*/ React.createElement("div", {
        className: mergedClassName,
        style: mergedStyle
    }, customOptions && customOptions.length > 1 && /*#__PURE__*/ React.createElement("div", {
        className: [
            "".concat(prefixCls, "-region-filter"),
            hashId
        ].filter(Boolean).join(' ')
    }, /*#__PURE__*/ React.createElement(Dropdown, {
        menu: {
            items: customOptions.map(function(item) {
                return {
                    key: item.key,
                    label: item.label,
                    disabled: item.key === selectedCustomSelection
                };
            }),
            onClick: function(param) {
                var key = param.key;
                return handleRegionChange(key);
            }
        },
        trigger: [
            'click'
        ],
        getPopupContainer: function() {
            return document.body;
        }
    }, /*#__PURE__*/ React.createElement(Button, {
        type: "default",
        size: "small",
        className: [
            "".concat(prefixCls, "-region-dropdown-btn"),
            hashId
        ].filter(Boolean).join(' ')
    }, /*#__PURE__*/ React.createElement("span", null, ((_customOptions_find = customOptions.find(function(r) {
        return r.key === selectedCustomSelection;
    })) === null || _customOptions_find === void 0 ? void 0 : _customOptions_find.label) || (i18n === null || i18n === void 0 ? void 0 : (_i18n_locale = i18n.locale) === null || _i18n_locale === void 0 ? void 0 : _i18n_locale.all) || '全部'), /*#__PURE__*/ React.createElement(ChevronDown, {
        className: [
            "".concat(prefixCls, "-dropdown-icon"),
            hashId
        ].filter(Boolean).join(' ')
    })))), filterOptions && filterOptions.length > 1 && /*#__PURE__*/ React.createElement(Segmented, {
        options: filterOptions || [],
        value: selectedFilter,
        size: "small",
        className: [
            "".concat(prefixCls, "-segmented-filter"),
            'custom-segmented',
            hashId
        ].filter(Boolean).join(' '),
        onChange: function(value) {
            return handleFilterChange(value);
        }
    })));
};
var ChartFilter = ChartFilterComponent;
export default ChartFilter;
