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 _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);
}
import { useEffect, useMemo, useState } from "react";
import { getDataHash } from "../utils";
var EMPTY_DATA = [];
/**
 * 图表数据筛选 Hook
 *
 * 用于管理图表数据的筛选逻辑，包括分类筛选和 filterLabel 筛选。
 * 自动处理数据变化时的筛选状态重置。
 *
 * @param {ChartDataItem[]} data - 原始数据数组
 * @returns {object} 包含筛选后的数据、筛选选项和筛选状态的对象
 *
 * @example
 * ```typescript
 * const {
 *   filteredData,
 *   categories,
 *   filterOptions,
 *   filterLabels,
 *   selectedFilter,
 *   setSelectedFilter,
 *   selectedFilterLabel,
 *   setSelectedFilterLabel,
 *   filteredDataByFilterLabel,
 * } = useChartDataFilter(data);
 * ```
 *
 * @since 1.0.0
 */ export var useChartDataFilter = function useChartDataFilter(data) {
    var safeData = Array.isArray(data) ? data : EMPTY_DATA;
    // 使用数据哈希来优化依赖项比较
    var dataHash = useMemo(function() {
        return getDataHash(safeData);
    }, [
        safeData
    ]);
    // 从数据中提取唯一的类别作为筛选选项
    var categories = useMemo(function() {
        var uniqueCategories = _to_consumable_array(new Set(safeData.map(function(item) {
            return item.category;
        }))).filter(Boolean);
        return uniqueCategories;
    }, [
        dataHash
    ]);
    // 从数据中提取 filterLabel，过滤掉 undefined 值
    var validFilterLabels = useMemo(function() {
        return safeData.map(function(item) {
            return item.filterLabel;
        }).filter(function(filterLabel) {
            return filterLabel !== undefined;
        });
    }, [
        dataHash
    ]);
    var filterLabels = useMemo(function() {
        return validFilterLabels.length > 0 ? _to_consumable_array(new Set(validFilterLabels)) : undefined;
    }, [
        validFilterLabels
    ]);
    // 状态管理
    var _useState = _sliced_to_array(useState(categories.find(Boolean) || ''), 2), selectedFilter = _useState[0], setSelectedFilter = _useState[1];
    var _useState1 = _sliced_to_array(useState(filterLabels && filterLabels.length > 0 ? filterLabels[0] : undefined), 2), selectedFilterLabel = _useState1[0], setSelectedFilterLabel = _useState1[1];
    // 当数据变化导致当前选中分类失效时，自动回退到首个有效分类或空（显示全部）
    useEffect(function() {
        if (selectedFilter && !categories.includes(selectedFilter)) {
            setSelectedFilter(categories.find(Boolean) || '');
        }
    }, [
        categories,
        selectedFilter
    ]);
    // 筛选数据
    var filteredData = useMemo(function() {
        var base = selectedFilter ? safeData.filter(function(item) {
            return item.category === selectedFilter;
        }) : safeData;
        var withFilterLabel = !filterLabels || !selectedFilterLabel ? base : base.filter(function(item) {
            return item.filterLabel === selectedFilterLabel;
        });
        // 统一过滤掉 x 为空（null/undefined）的数据，避免后续 toString 报错
        return withFilterLabel.filter(function(item) {
            return item.x !== null && item.x !== undefined;
        });
    }, [
        dataHash,
        selectedFilter,
        filterLabels,
        selectedFilterLabel
    ]);
    // 筛选器选项
    var filterOptions = useMemo(function() {
        return categories.map(function(category) {
            return {
                label: category || '默认',
                value: category || '默认'
            };
        });
    }, [
        categories
    ]);
    // 根据 filterLabel 筛选数据 - 只有当 filterLabels 存在时才生成
    var filteredDataByFilterLabel = useMemo(function() {
        return filterLabels === null || filterLabels === void 0 ? void 0 : filterLabels.map(function(item) {
            return {
                key: item,
                label: item
            };
        });
    }, [
        filterLabels
    ]);
    return {
        filteredData: filteredData,
        categories: categories,
        filterOptions: filterOptions,
        filterLabels: filterLabels,
        selectedFilter: selectedFilter,
        setSelectedFilter: setSelectedFilter,
        selectedFilterLabel: selectedFilterLabel,
        setSelectedFilterLabel: setSelectedFilterLabel,
        filteredDataByFilterLabel: filteredDataByFilterLabel,
        safeData: safeData
    };
};
