import store from '@/store';
const userId = store.state.user?.info?.userId;
const userName = store.state.user?.info?.name;
export function isPower(data = {}, power, selection = []) {
  if (power == 'add' && data.userAuthority.length == 0) {
    //文件夹默认开放新增权限
    return true;
  }
  if (!data?.id && selection.length == 0) {
    return false;
  }
  if (power == 'checkEnter' && data?.checkOutUserId != userId) {
    //检入特殊处理
    return false;
  }
  if (selection.length > 0) {
    //多选情况处理
    let isPower = true;
    let userAuthorityS = [];
    let createUserIdS = selection
      .map((item) => item.createUserId)
      .filter((createUserId) => createUserId == userId);
    if (createUserIdS.length == selection.length || userName == 'admin') {
      return true;
    }
    selection.forEach((val) => {
      const powerObj = val.userAuthority.find((item) => item.id == userId);
      if (powerObj) {
        userAuthorityS.push(powerObj);
      }
    });

    if (userAuthorityS.length == 0) {
      isPower = false;
    }
    userAuthorityS.forEach((item) => {
      if (item[power] != '1') {
        isPower = false;
      }
    });
    return isPower;
  }

  if (data.createUserId == userId || userName == 'admin') {
    //创建人/admin所有权限
    return true;
  }
  if (!power) {
    return false;
  }
  const powerObj = data.userAuthority.find((item) => item.id == userId);
  if (powerObj) {
    return powerObj[power] == '1';
  }
}
export function setFileUrl(row) {
  let file = row.storagePath[0];
  let fileNames = file.storePath.split('/');
  let url =
    window.location.origin +
    '/api/main/file/getFile?objectName=' +
    file.storePath +
    '&fullfilename=' +
    fileNames[fileNames.length - 1];
  return '/kkfile/onlinePreview?url=' + btoa(url);
}
export function setFolderList(data) {
  //递归过滤文件夹权限
  data.forEach((item) => {
    item['disabled'] = !isPower(item, 'add');
    if (item.sonDirectoryList && item.sonDirectoryList.length > 0) {
      setFolderList(item.sonDirectoryList);
    }
  });
}
//新迪预览工具支持的文件格式
export function getFileType() {
  return [
    'par',
    'asm',
    'psm',
    'dft',
    'sldprt',
    'sldasm',
    'ipt',
    'iam',
    'prt',
    '3dxml',
    'CATPart',
    'CATProduct',
    'cgr',
    'model',
    'exp',
    'session',
    'x_t',
    'xmt_txt',
    'x_b',
    'xmp_bin',
    'xmp_txt',
    'sat',
    'sab',
    'igs',
    'iges',
    'stp',
    'step',
    'jt',
    'xcgm',
    '3dm',
    'stl',
    'obj',
    '3mf',
    'fbx',
    'vda',
    'dxf',
    'dwg',
    'pdf',
    'idf',
    'idb',
    'emn',
    'brd'
  ];
}
