"use strict";

var _interopRequireDefault = require("F:/GIT/opu-components-vue/node_modules/@babel/runtime/helpers/interopRequireDefault.js").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _toConsumableArray2 = _interopRequireDefault(require("F:/GIT/opu-components-vue/node_modules/@babel/runtime/helpers/esm/toConsumableArray.js"));

require("core-js/modules/es.array.concat.js");

require("core-js/modules/es.regexp.exec.js");

require("core-js/modules/es.string.split.js");

require("core-js/modules/es.array.filter.js");

require("core-js/modules/es.object.to-string.js");

require("core-js/modules/es.array.includes.js");

require("core-js/modules/es.string.includes.js");

require("core-js/modules/es.error.cause.js");

require("core-js/modules/es.string.replace.js");

require("core-js/modules/es.array.map.js");

require("core-js/modules/es.json.stringify.js");

require("core-js/modules/es.regexp.to-string.js");

require("core-js/modules/es.number.constructor.js");

var _xeUtils = _interopRequireDefault(require("xe-utils"));

//  查询当前options中 是否存在指定值
function _hasOptionsValue(options, value, valueField, childrenField) {
  var is = false;

  for (var i = 0; i < options.length; i++) {
    var item = options[i];

    if (item[valueField] == value) {
      is = true;
    } else if (item[childrenField] && item[childrenField].length > 0) {
      is = _hasOptionsValue(item[childrenField], value, valueField, childrenField);
    }

    if (is) break;
  }

  return is;
}

function hasOptionsMultipleValue(options, value, valueField, childrenField) {
  var existValue = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [];

  for (var i = 0; i < options.length; i++) {
    var item = options[i];

    if (value.indexOf(item[valueField]) > -1) {
      existValue.push(item[valueField]);
    }

    if (item[childrenField] && item[childrenField].length > 0) {
      existValue = existValue.concat(hasOptionsMultipleValue(item[childrenField], value, valueField, childrenField));
    }

    if (existValue.length == value.length) break;
  }

  return existValue;
}

_xeUtils.default.mixin({
  // 获取对象中指定key的值
  getObjData: function getObjData(key, obj) {
    var value = obj;

    if (key && key.indexOf(".") > -1) {
      var keysArr = key.split(".");

      for (var i = 0; i < keysArr.length; i++) {
        var k = keysArr[i];

        if (value[k] !== undefined) {
          value = value[k];
        } else {
          break;
        }
      }
    } else if (key) {
      value = value[key];
    }

    return value;
  },

  /**
   * @description: 对象中指定key的值 更改
   * @param {*} key
   * @param {*} obj
   * @param {*} value
   * @return {*}
   */
  setObjData: function setObjData(key, obj, value) {
    var target = obj;

    if (key && key.indexOf(".") > -1) {
      var keysArr = key.split(".");

      for (var i = 0; i < keysArr.length; i++) {
        var k = keysArr[i];

        if (i + 1 == keysArr.length) {
          target[k] = value;
        } else {
          target = target[k];
        }
      }
    } else if (key) {
      target[key] = value;
    }
  },

  /**
   * 判断两个数组中是否存在相同值
   * @param {*} array
   * @param {*} array1
   */
  hasEquaValueArray: function hasEquaValueArray(array, array1) {
    if (_xeUtils.default.isArray(array) && _xeUtils.default.isArray(array1)) {
      var equaValues = array.filter(function (p) {
        return array1.includes(p);
      });

      if (equaValues.length) {
        return true;
      }
    }

    return false;
  },

  /**
   * 将字符串复制到剪切板
   * @param {*} text
   */
  copyTextToClipboard: function copyTextToClipboard(text) {
    var element = document.createElement("textarea");
    var previouslyFocusedElement = document.activeElement;
    element.value = text;
    element.setAttribute("readonly", "");
    element.style.contain = "strict";
    element.style.position = "absolute";
    element.style.left = "-9999px";
    element.style.fontSize = "12pt";
    var selection = document.getSelection();
    var originalRange;

    if (selection && selection.rangeCount > 0) {
      originalRange = selection.getRangeAt(0);
    }

    document.body.append(element);
    element.select();
    element.selectionStart = 0;
    element.selectionEnd = text.length;
    var isSuccess = false;

    try {
      isSuccess = document.execCommand("copy");
    } catch (e) {
      throw new Error(e);
    }

    element.remove();

    if (originalRange && selection) {
      selection.removeAllRanges();
      selection.addRange(originalRange);
    }

    if (previouslyFocusedElement) {
      previouslyFocusedElement.focus();
    }

    return isSuccess;
  },
  // 将指定字符（下划线）转换为首字母小写驼峰
  lineToUpperCase: function lineToUpperCase(str) {
    var sign = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "_";
    var arr = str.split(sign);
    var newStr = "";

    if (arr && arr.length) {
      for (var i = 0; i < arr.length; i++) {
        var p = arr[i];
        newStr += i > 0 ? p.replace(p[0], p[0].toUpperCase()) : p;
      }
    } else {
      newStr = str;
    }

    return newStr;
  },
  // 树形结构转为数组
  treeTransArray: function treeTransArray(tree) {
    var _ref;

    var key = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "children";
    return (_ref = []).concat.apply(_ref, (0, _toConsumableArray2.default)(tree.map(function (item) {
      if (item[key] && item[key].length) {
        var currentItem = _xeUtils.default.clone(item, true);

        delete currentItem[key];
        return [].concat(currentItem, _xeUtils.default.treeTransArray(item[key], key));
      } else {
        return item;
      }
    })));
  },
  hasOptionsValue: function hasOptionsValue(options, value, valueField, childrenField, isMultiple) {
    if (isMultiple) {
      return hasOptionsMultipleValue(options, value, valueField, childrenField);
    } else {
      return _hasOptionsValue(options, value, valueField, childrenField);
    }
  },
  //是否空对象
  isEmptyObject: function isEmptyObject(data) {
    return JSON.stringify(data) === "{}";
  },

  /**
   * 生成一个 不重复的ID
   * @param { Number } randomLength
   */
  getUid: function getUid() {
    var randomLength = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 36;
    var dateTime = Date.now().toString();
    return Number(Math.random().toString().substr(2, randomLength) + dateTime).toString(36);
  }
});

var _default = _xeUtils.default;
exports.default = _default;