import { useClipboard } from '@vueuse/core';
import { ElMessage } from 'element-plus';
import { i18n } from '../locale/index.mjs';
import '../utils/index.mjs';
import '../stores/index.mjs';
import '../api/index.mjs';
import { formatDate } from '../utils/formatTime.mjs';
import { useBaseApi } from '../api/base/index.mjs';
import { useThemeConfig } from '../stores/themeConfig.mjs';

function commonFunction() {
  const { copy, isSupported } = useClipboard();
  const percentFormat = (row, column, cellValue) => {
    return cellValue ? `${cellValue}%` : "-";
  };
  const dateFormatYMD = (row, column, cellValue) => {
    if (!cellValue) return "-";
    return formatDate(new Date(cellValue), "YYYY-mm-dd");
  };
  const dateFormatYMDHMS = (row, column, cellValue) => {
    if (!cellValue) return "-";
    return formatDate(new Date(cellValue), "YYYY-mm-dd HH:MM:SS");
  };
  const dateFormatHMS = (row, column, cellValue) => {
    if (!cellValue) return "-";
    let time = 0;
    if (typeof row === "number") time = row;
    if (typeof cellValue === "number") time = cellValue;
    return formatDate(new Date(time * 1e3), "HH:MM:SS");
  };
  const scaleFormat = (value = "0", scale = 4) => {
    return Number.parseFloat(value).toFixed(scale);
  };
  const scale2Format = (value = "0") => {
    return Number.parseFloat(value).toFixed(2);
  };
  const groupSeparator = (value, minimumFractionDigits = 2) => {
    return value.toLocaleString("en-US", {
      minimumFractionDigits,
      maximumFractionDigits: 2
    });
  };
  const copyText = (text) => {
    return new Promise((resolve, reject) => {
      try {
        if (!isSupported.value) {
          console.error("\u6D4F\u89C8\u5668\u4E0D\u652F\u6301\u526A\u8D34\u677F API");
          reject("\u6D4F\u89C8\u5668\u4E0D\u652F\u6301\u526A\u8D34\u677F API");
        } else {
          copy(text);
          ElMessage.success(i18n.global.t("message.layout.copyTextSuccess"));
          resolve(text);
        }
      } catch (e) {
        ElMessage.error(i18n.global.t("message.layout.copyTextError"));
        reject(e);
      }
    });
  };
  const removeHtmlSub = (value) => {
    var str = value.replace(/<[^>]+>/g, "");
    if (str.length > 50) return str.substring(0, 50) + "......";
    else return str;
  };
  const removeHtml = (value) => {
    return value.replace(/<[^>]+>/g, "");
  };
  const getEnumDesc = (key, lstEnum) => {
    var _a;
    return (_a = lstEnum.find((x) => x.value == key)) == null ? void 0 : _a.describe;
  };
  const appendQueryParams = (url, params) => {
    if (!params || Object.keys(params).length == 0) return url;
    const queryString = Object.keys(params).map((key) => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`).join("&");
    return `${url}${url.includes("?") ? "&" : "?"}${queryString}`;
  };
  const getNameAbbr = (text, callback) => {
    if (!text) return ElMessage.error("\u83B7\u53D6\u7B80\u79F0\u6587\u672C\u4E0D\u80FD\u4E3A\u7A7A");
    try {
      return useBaseApi("sysCommon").post({ text }, "nameAbbr").then((res) => {
        if (callback) callback(res.data.result);
        return res.data.result;
      });
    } catch (e) {
      ElMessage.error("\u83B7\u53D6\u5931\u8D25");
    }
  };
  const handleConditionalClear = (condition, fieldValue, clearValue = void 0) => {
    if (condition) {
      return clearValue;
    }
    return fieldValue;
  };
  const getTimeRangePickerShortcuts = () => {
    const themeStore = useThemeConfig();
    return [
      {
        text: "\u8FD1\u4E00\u5468",
        value: () => {
          const now = new Date(themeStore.themeConfig.serverTime);
          const end = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59, 999);
          const start = new Date(now.getFullYear(), now.getMonth(), now.getDate() - 7);
          return [start, end];
        }
      },
      {
        text: "\u8FD1\u4E00\u6708",
        value: () => {
          const now = new Date(themeStore.themeConfig.serverTime);
          const end = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59, 999);
          const start = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate());
          return [start, end];
        }
      },
      {
        text: "\u8FD1\u4E09\u6708",
        value: () => {
          const now = new Date(themeStore.themeConfig.serverTime);
          const end = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59, 999);
          const start = new Date(now.getFullYear(), now.getMonth() - 3, now.getDate());
          return [start, end];
        }
      },
      {
        text: "\u8FD1\u534A\u5E74",
        value: () => {
          const now = new Date(themeStore.themeConfig.serverTime);
          const end = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59, 999);
          const start = new Date(now.getFullYear(), now.getMonth() - 6, now.getDate());
          return [start, end];
        }
      },
      {
        text: "\u672C\u5E74",
        value: () => {
          const now = new Date(themeStore.themeConfig.serverTime);
          const end = new Date(now.getFullYear(), 11, 31, 23, 59, 59, 999);
          const start = new Date(now.getFullYear(), 0, 1);
          return [start, end];
        }
      },
      {
        text: "\u8FD1\u4E24\u5E74",
        value: () => {
          const now = new Date(themeStore.themeConfig.serverTime);
          const end = new Date(now.getFullYear(), 11, 31, 23, 59, 59, 999);
          const start = new Date(now.getFullYear() - 1, 0, 1);
          return [start, end];
        }
      },
      {
        text: "\u8FD1\u4E09\u5E74",
        value: () => {
          const now = new Date(themeStore.themeConfig.serverTime);
          const end = new Date(now.getFullYear(), 11, 31, 23, 59, 59, 999);
          const start = new Date(now.getFullYear() - 2, 0, 1);
          return [start, end];
        }
      }
    ];
  };
  return {
    percentFormat,
    dateFormatYMD,
    dateFormatYMDHMS,
    dateFormatHMS,
    scaleFormat,
    scale2Format,
    groupSeparator,
    copyText,
    removeHtmlSub,
    removeHtml,
    getEnumDesc,
    appendQueryParams,
    getNameAbbr,
    handleConditionalClear,
    getTimeRangePickerShortcuts
  };
}

export { commonFunction as default };
