import Iconfont from '@/components/IconSelect/iconfont';
import { message } from 'antd';
import { getToken, setAuthority } from '@/utils/auth';

export function copy(text) {
  let oInput = document.createElement('input')
  oInput.value = text;
  document.body.appendChild(oInput)
  oInput.select() // 选择对象
  document.execCommand("Copy") // 执行浏览器复制命令
  message.success('复制成功');
  oInput.remove()
}

//下载连接
export function exportLink(searchParams, api) {
  searchParams.token = getToken();
  const params = Object.entries(searchParams).length ? `?${new URLSearchParams(searchParams)}` : '';
  return `${process.env.API_URL}${api}${params}`;
};

export function formatMenuData(data) {
  const items = [];
  data.forEach((item) => {
    const menu = {
      path: item.path,
      name: item.name,
      hideInMenu: item.type > 1,
      icon: item.icon ? <Iconfont type={item.icon}
      /> : ' ',
      children: []
    };

    if (item.children && item.type === 1 && item.children.length) {
      menu.children = formatMenuData(item.children)
    }
    if (item.type === 1) {
      items.push(menu);
    }
  });
  return items;
};

/*
 * @Note:
 * @Author: 2058827620@qq.com
 * @Date: 2022-07-31 19:24:14
 */
export function deepCopy(data) {
  if (data.constructor.name === 'Array') {
    // 判断为数组类型
    const arrCopy = []
    for (let i = 0, len = data.length; i < len; i++) {
      //遍历数组
      if (data[i] instanceof Object) {
        arrCopy.push(deepCopy(data[i]))
      } else {
        // 基本类型
        arrCopy.push(data[i])
      }
    }
    return arrCopy;

  } else { // 为对象
    const objCopy = {};
    for (let x in data) {
      if (data[x] instanceof Object) {
        objCopy[x] = deepCopy(data[x])
      } else { // 基本类型
        objCopy[x] = data[x];
      }
    }
    return objCopy;
  }
}
