"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.removeNoPermission = exports.btnIsHide = exports.btnIsDisabled = exports.btnClickFunc = void 0;
var _lodash = _interopRequireDefault(require("lodash"));
var _index = require("../data/index");
var _excluded = ["name"];
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } // @ts-nocheck
// 按钮方法
var btnClickFunc = exports.btnClickFunc = function btnClickFunc(fn) {
  var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  if (!fn) return null;
  var paramsArr = [];
  if (_typeof(params) === 'object') {
    Object.keys(params).map(function (item) {
      paramsArr.push(params[item]);
    });
  }
  if (typeof fn === 'function') {
    return fn.apply(void 0, paramsArr); // 表格列返回的是方法
  }
  return fn;
};
// 按钮是否禁用
var btnIsDisabled = exports.btnIsDisabled = function btnIsDisabled(isDisabled) {
  var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  if (!isDisabled) return false;
  var paramsArr = [];
  if (_typeof(params) === 'object') {
    Object.keys(params).map(function (item) {
      paramsArr.push(params[item]);
    });
  }
  if (typeof isDisabled === 'function') {
    return isDisabled.apply(void 0, paramsArr); // 表格列返回的是方法
  }
  return isDisabled;
};
// 按钮是否隐藏
var btnIsHide = exports.btnIsHide = function btnIsHide(isHide) {
  var params = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  if (!isHide) return false;
  var paramsArr = [];
  if (_typeof(params) === 'object') {
    Object.keys(params).map(function (item) {
      paramsArr.push(params[item]);
    });
  }
  if (typeof isHide === 'function') {
    return isHide.apply(void 0, paramsArr); // 表格列返回的是方法
  }
  return isHide;
};
// 重新组合数据(排除不包含在permissionCodes内及isHide数据
var removeNoPermission = exports.removeNoPermission = function removeNoPermission(array, permissionCodes, codesList, params) {
  // 合并本地的codes
  var codes = _lodash.default.unionBy(codesList || [], _index.funcCode.btnFuncCodes, 'code');
  return Array.isArray(array) && array.filter(function (item) {
    if (item.children) {
      item.children = removeNoPermission(item.children, permissionCodes, codes, params);
      if (item.children.length === 0) return false; // 如果子集合过滤为空时，不返当前数据
    }
    // permissionCodes未定义，则直接返回不进行过滤
    if (item.hasOwnProperty('code') && permissionCodes) {
      // if (!permissionCodes) return true //  permissionCodes未定义，则直接返回不进行过滤
      // 判断是否包含 'code'字段，如果包含则先过滤有在 permissionCodes内的(permissionCodes数组为0时，也不返回数据)
      // 并用是不隐藏的条件
      return permissionCodes.length !== 0 && permissionCodes.includes(item.code) && !btnIsHide(item.isHide, params);
    }
    if (item.hasOwnProperty('isHide')) {
      // 判断是否包含 'isHide'字段，如果包含则先过滤 isHide 为false
      return !btnIsHide(item.isHide, params);
    }
    return true;
  }).map(function (resultItem) {
    // 与codes进行对比，相应的 code匹配以对应的name,icon,code返回
    // 如果有配置code，则使用func.code.js里面配置的名称
    if (codes && resultItem.code) {
      var matchIndex = _lodash.default.findIndex(codes, function (btnItem) {
        return btnItem.code === resultItem.code;
      });
      if (matchIndex !== -1) {
        var _codes$matchIndex;
        // 如果有配置code，则使用func.code.js里面配置的名称；如果exName为null时，则不显示文本名称
        var _ref = codes[matchIndex] || {},
          name = _ref.name,
          matchCode = _objectWithoutProperties(_ref, _excluded);
        var reName = resultItem.exName || name || resultItem.name;
        if (resultItem.hasOwnProperty('exName') && !resultItem.exName) {
          reName = null;
        }
        resultItem = _objectSpread(_objectSpread(_objectSpread({}, resultItem), matchCode), {}, {
          name: reName,
          icon: resultItem.icon === '' || resultItem.icon === null ? resultItem.icon : resultItem.icon || ((_codes$matchIndex = codes[matchIndex]) === null || _codes$matchIndex === void 0 ? void 0 : _codes$matchIndex.icon)
        });
      }
    }
    return resultItem;
  }) || [];
};