// import { getAction } from '@/api/flowable/manage';

import store from '@/store';

export function formatDictText(dicts, values) {
  if (!(Array.isArray(dicts) && dicts.length > 0)) {
    return values;
  }
  if (!values) {
    return '';
  }
  let valueArr = values.split(',');
  let contentArr = [];
  dicts.forEach((dict) => {
    for (let i = 0; i < valueArr.length; i++) {
      if (valueArr[i] === dict.value) {
        contentArr.push(dict.content);
        break;
      }
    }
  });
  return contentArr.toString();
}

export function randomString(len) {
  len = len || 32;
  const $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';
  /** **默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
  const maxPos = $chars.length;
  let pwd = '';
  for (let i = 0; i < len; i++) {
    pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
  }
  return pwd;
}
//不合格品台账状态
export function unacceptedProductStatus(v) {
  let status = '';
  switch (v) {
    case 0:
      status = '待处理';
      break;
    case 1:
      status = '处理中';
      break;
    case 2:
      status = '已处理';
      break;
    case 3:
      status = '已完结';
      break;
  }
  return status;
}
//返工返修状态
export function reworkStatus(v) {
  let status = '';
  switch (v) {
    case 1:
      status = '待处理';
      break;
    case 2:
      status = '处理中';
      break;
    case 3:
      status = '已完成';
      break;
  }
  return status;
}
export function hangingStatus(v) {
  let status = '';
  switch (v) {
    case 0:
      status = '未发布';
      break;
    case 1:
      status = '待生产';
      break;
    case 2:
      status = '生产中';
      break;
    case 10:
      status = '已完结';
      break;
  }
  return status;
}

export function deepCopy(obj, hash = new WeakMap()) {
  if (obj === null) return null;
  if (obj instanceof Date) return new Date(obj);
  if (obj instanceof RegExp) return new RegExp(obj);
  if (typeof obj !== 'object' && typeof obj !== 'function') return obj;
  if (hash.has(obj)) return hash.get(obj);

  const result = Array.isArray(obj) ? [] : {};
  hash.set(obj, result);

  return Object.keys(obj).reduce((acc, key) => {
    acc[key] = this.deepCopy(obj[key], hash);
    return acc;
  }, result);
}

/**
 * 防抖函数
 * @param {Function} func - 需要执行的函数
 * @param {number} wait - 延迟时间 (ms)
 * @param {boolean} immediate - 是否立即执行
 * @returns {Function}
 */
export function debounce(func, wait = 300, immediate = false) {
  let timer = null;
  return function (...args) {
    const context = this;

    if (timer) clearTimeout(timer);

    if (immediate && !timer) {
      func.apply(context, args);
    }

    timer = setTimeout(() => {
      func.apply(context, args);
      timer = null;
    }, wait);
  };
}

/**
 * 节流函数
 * @param {Function} func - 需要执行的函数
 * @param {number} wait - 间隔时间 (ms)
 * @returns {Function}
 */
export function throttle(func, wait = 300) {
  let lastTime = 0;
  return function (...args) {
    const context = this;
    const now = Date.now();
    if (now - lastTime >= wait) {
      func.apply(context, args);
      lastTime = now;
    }
  };
}
