'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var useClipboard = require('vue-clipboard3');
var elementPlus = require('element-plus');
var formatTime = require('./formatTime.js');
var vueI18n = require('vue-i18n');

function commonFunction() {
  const { toClipboard } = useClipboard();
  const percentFormat = (row, column, cellValue) => {
    return cellValue ? `${cellValue}%` : "-";
  };
  const dateFormatYMD = (row, column, cellValue) => {
    if (!cellValue) return "-";
    return formatTime.formatDate(new Date(cellValue), "YYYY-mm-dd");
  };
  const dateFormatYMDHMS = (row, column, cellValue) => {
    if (!cellValue) return "-";
    return formatTime.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 formatTime.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) => {
    const { t } = vueI18n.useI18n();
    return new Promise((resolve, reject) => {
      try {
        toClipboard(text);
        elementPlus.ElMessage.success(t("message.layout.copyTextSuccess"));
        resolve(text);
      } catch (e) {
        elementPlus.ElMessage.error(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) => {
    return lstEnum.find((x) => x.value == key)?.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}`;
  };
  return {
    percentFormat,
    dateFormatYMD,
    dateFormatYMDHMS,
    dateFormatHMS,
    scaleFormat,
    scale2Format,
    groupSeparator,
    copyText,
    removeHtmlSub,
    removeHtml,
    getEnumDesc,
    appendQueryParams
  };
}

exports.default = commonFunction;
