'use strict';

var userInfo = require('../stores/userInfo.js');
var arrayOperation = require('../utils/arrayOperation.js');

function authDirective(app) {
  app.directive("auth", {
    mounted(el, binding) {
      const stores = userInfo.useUserInfo();
      if (!stores.userInfos.authApiList.some((v) => v === binding.value)) el.parentNode.removeChild(el);
    }
  });
  app.directive("auths", {
    mounted(el, binding) {
      let flag = false;
      const stores = userInfo.useUserInfo();
      stores.userInfos.authApiList.map((val) => {
        binding.value.map((v) => {
          if (val === v) flag = true;
        });
      });
      if (!flag) el.parentNode.removeChild(el);
    }
  });
  app.directive("auth-all", {
    mounted(el, binding) {
      const stores = userInfo.useUserInfo();
      const flag = arrayOperation.judgementSameArr(binding.value, stores.userInfos.authApiList);
      if (!flag) el.parentNode.removeChild(el);
    }
  });
  app.directive("hdtable", {
    mounted(el, binding) {
      const stores = userInfo.useUserInfo();
      const config = typeof binding.value === "string" ? { path: binding.value, field: "" } : binding.value;
      const item = stores.userInfos.formDes.find((item2) => item2.path === config.path);
      if (item && item.formDes.length > 0) {
        const xitem = item.formDes.find((v) => v.field === config.field);
        if (xitem && xitem.isTable == 0) el.parentNode.removeChild(el);
      }
    }
  });
  app.directive("hpquery", {
    mounted(el, binding) {
      const stores = userInfo.useUserInfo();
      const config = typeof binding.value === "string" ? { path: binding.value, field: "" } : binding.value;
      const item = stores.userInfos.formDes.find((item2) => item2.path === config.path);
      if (item && item.formDes.length > 0) {
        const xitem = item.formDes.find((v) => v.field === config.field);
        if (xitem && xitem.isQuery == 0) el.parentNode.removeChild(el);
      }
    }
  });
  app.directive("hpaddupdate", {
    mounted(el, binding) {
      const stores = userInfo.useUserInfo();
      const config = typeof binding.value === "string" ? { path: binding.value, field: "" } : binding.value;
      const item = stores.userInfos.formDes.find((item2) => item2.path === config.path);
      if (item && item.formDes.length > 0) {
        const xitem = item.formDes.find((v) => v.field === config.field);
        if (xitem && xitem.isAddUpdate == 0) el.parentNode.removeChild(el);
      }
    }
  });
}

exports.authDirective = authDirective;
