'use strict';

"use strict";
const paginateTableData = ({ data, currentPage, pageSize }) => {
  if (!Array.isArray(data)) return [];
  if (typeof currentPage !== "number" || currentPage < 1) return [];
  if (typeof pageSize !== "number" || pageSize < 1) return [];
  const startIndex = (currentPage - 1) * pageSize;
  const endIndex = startIndex + pageSize;
  if (startIndex >= data.length) return [];
  return data.slice(startIndex, endIndex);
};
const searchTableData = ({
  data,
  queries,
  caseSensitive = false,
  matchAll = true
}) => {
  if (!Array.isArray(data)) return [];
  if (data.length === 0) return [];
  if (!queries || typeof queries !== "object") return data;
  if (Object.keys(queries).length === 0) return data;
  const validQueries = Object.entries(queries).reduce((acc, [field, value]) => {
    if (value != null && value !== "" && value !== void 0) {
      acc[field] = value;
    }
    return acc;
  }, {});
  if (Object.keys(validQueries).length === 0) return data;
  return data.filter((item) => {
    if (item == null) return false;
    const conditionMatches = Object.entries(validQueries).map(
      ([field, queryValue]) => {
        if (!Object.prototype.hasOwnProperty.call(item, field)) {
          return false;
        }
        const itemValue = item[field];
        if (itemValue == null) return false;
        const searchValue = caseSensitive ? String(queryValue) : String(queryValue).toLowerCase();
        const itemValueStr = caseSensitive ? String(itemValue) : String(itemValue).toLowerCase();
        return itemValueStr.includes(searchValue);
      }
    );
    return matchAll ? conditionMatches.every((match) => match) : conditionMatches.some((match) => match);
  });
};
const sortTableData = ({ data, sortBy }) => {
  if (!Array.isArray(data)) return [];
  if (!Array.isArray(sortBy) || sortBy.length === 0) return data;
  if (data.length === 0) return [];
  const sortedData = [...data];
  const detectType = (value) => {
    if (value == null) return "null";
    if (!isNaN(Number(value)) && value !== "") {
      const num = Number(value);
      if (typeof value === "number" || typeof value === "string" && String(num) === value.trim()) {
        return "number";
      }
    }
    if (typeof value === "string") {
      const dateTimePatterns = [
        /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/,
        // 2024-01-15 10:30:00
        /^\d{4}-\d{2}-\d{2}$/,
        // 2024-01-15
        /^\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}$/,
        // 2024/01/15 10:30:00
        /^\d{4}\/\d{2}\/\d{2}$/,
        // 2024/01/15
        /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/
        // ISO格式
      ];
      for (const pattern of dateTimePatterns) {
        if (pattern.test(value.trim())) {
          const date = new Date(value);
          if (!isNaN(date.getTime())) {
            return "datetime";
          }
        }
      }
    }
    return "string";
  };
  const createSmartComparator = (field) => {
    const samples = data.filter((item) => item[field] != null).slice(0, 10).map((item) => item[field]);
    if (samples.length === 0) return "string";
    const typeCounts = {};
    samples.forEach((sample) => {
      const type = detectType(sample);
      typeCounts[type] = (typeCounts[type] || 0) + 1;
    });
    return Object.keys(typeCounts).reduce(
      (a, b) => typeCounts[a] > typeCounts[b] ? a : b
    );
  };
  const fieldTypes = {};
  sortBy.forEach((config) => {
    fieldTypes[config.field] = createSmartComparator(config.field);
  });
  const compareValues = (a, b, fieldConfig) => {
    const { field, order = "asc" } = fieldConfig;
    const type = fieldTypes[field];
    const valueA = a[field];
    const valueB = b[field];
    if (valueA == null && valueB == null) return 0;
    if (valueA == null) return order === "asc" ? -1 : 1;
    if (valueB == null) return order === "asc" ? 1 : -1;
    switch (type) {
      case "number":
        return compareNumbers(valueA, valueB, order);
      case "datetime":
        return compareDatetimes(valueA, valueB, order);
      case "string":
      default:
        return compareStrings(valueA, valueB, order);
    }
  };
  const compareNumbers = (a, b, order) => {
    const numA = Number(a);
    const numB = Number(b);
    if (isNaN(numA) && isNaN(numB)) return 0;
    if (isNaN(numA)) return order === "asc" ? -1 : 1;
    if (isNaN(numB)) return order === "asc" ? 1 : -1;
    const comparison = numA - numB;
    return order === "asc" ? comparison : -comparison;
  };
  const compareDatetimes = (a, b, order) => {
    const dateA = new Date(a);
    const dateB = new Date(b);
    if (isNaN(dateA.getTime()) && isNaN(dateB.getTime())) {
      return compareStrings(a, b, order);
    }
    if (isNaN(dateA.getTime())) return order === "asc" ? -1 : 1;
    if (isNaN(dateB.getTime())) return order === "asc" ? 1 : -1;
    const timeA = dateA.getTime();
    const timeB = dateB.getTime();
    if (timeA < timeB) return order === "asc" ? -1 : 1;
    if (timeA > timeB) return order === "asc" ? 1 : -1;
    return 0;
  };
  const compareStrings = (a, b, order) => {
    const strA = String(a).toLowerCase();
    const strB = String(b).toLowerCase();
    if (strA < strB) return order === "asc" ? -1 : 1;
    if (strA > strB) return order === "asc" ? 1 : -1;
    return 0;
  };
  sortedData.sort((a, b) => {
    for (const sortConfig of sortBy) {
      const result = compareValues(a, b, sortConfig);
      if (result !== 0) return result;
    }
    return 0;
  });
  return sortedData;
};
const processStaticData = ({
  data,
  search,
  sort,
  paginate
}) => {
  if (!Array.isArray(data)) {
    return {
      items: [],
      total: 0
    };
  }
  let items = [...data];
  let filteredTotal = data.length;
  const shouldSearch = search && Object.keys(search.queries).length >= 0;
  if (shouldSearch) {
    items = searchTableData({
      data: items,
      queries: search.queries,
      caseSensitive: search.caseSensitive,
      matchAll: search.matchAll
    });
    filteredTotal = items.length;
  }
  const shouldSort = sort && Array.isArray(sort) && sort.length > 0;
  if (shouldSort) {
    const sortBy = sort.map((item) => ({
      order: item.order || "asc",
      ...item
    }));
    items = sortTableData({
      data: items,
      sortBy
    });
  }
  const shouldPaginate = paginate != null;
  if (shouldPaginate) {
    const current = Math.max(1, paginate.current || 1);
    const pageSize = Math.max(1, paginate.pageSize || 10);
    items = paginateTableData({
      data: items,
      currentPage: current,
      pageSize
    });
  }
  return {
    items,
    total: filteredTotal
    // 过滤后的总数据量
  };
};

exports.paginateTableData = paginateTableData;
exports.processStaticData = processStaticData;
exports.searchTableData = searchTableData;
exports.sortTableData = sortTableData;
//# sourceMappingURL=hooks.js.map
