function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

/**
 * @author ale0720@163.com
 * @date 2019-05-09 11:11
 */
import jsYaml from 'js-yaml';
/**
 *
 * @param value
 * @returns {boolean}
 * @private
 */

function _isObject(value) {
  return Object.prototype.toString.call(value) === '[object Object]';
}
/**
 * 判断对象是否为空
 * @param value
 * @returns {boolean}
 */


function _isEmpty(value) {
  return !Object.keys(value).length;
}
/**
 * 键值对模式下的一个 config 项
 * @param key
 * @param value
 * @param index 默认从1开始的数字，可以是字符串
 * @constructor
 */


function ConfigNode() {
  var key = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  var value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  var index = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
  if (!(this instanceof ConfigNode ? this.constructor : void 0)) throw new Error('ConfigNode() must be called with new');
  this.temp = '=';
  this.key = key;
  this.value = value;
  this.index = index;
}
/**
 * 对象转 YAML
 * 使用 js-yaml 的 dump 方法
 * @param value
 */


function objToYaml(value) {
  if (!value) return '';

  var _value = takeObject(value);

  if (_isEmpty(_value)) return '';
  return jsYaml.dump(_value);
}
/**
 * YAML 转对象
 * 使用 js-yaml 的 load 方法
 * @param value
 */


function yamlToObj(value) {
  return jsYaml.load(value);
}
/**
 * 根据配置映射键值对生成的数据结构提取出映射
 * @param data
 */


function takeObject(data) {
  var _value = {};
  if (_isObject(data)) return data;
  if (!Array.isArray(data)) return _value;

  for (var i = 0, len = data.length; i < len; i++) {
    var _data$i = data[i],
        key = _data$i.key,
        value = _data$i.value;

    if (key) {
      _value[key] = value || '';
    }
  }

  return _value;
}
/**
 * 由 YAML 数据 还原为键值对原始存储
 * @param data
 * @returns {*}
 */


function makePostData(data) {
  if (!data) {
    var config = new ConfigNode();
    return [config];
  }

  var dataEntries = Object.entries(data);
  var index = 1;
  var result = []; // eslint-disable-next-line no-restricted-syntax

  for (var _i = 0, _dataEntries = dataEntries; _i < _dataEntries.length; _i++) {
    var _dataEntries$_i = _slicedToArray(_dataEntries[_i], 2),
        key = _dataEntries$_i[0],
        value = _dataEntries$_i[1];

    var _config = new ConfigNode(key, value, index);

    result.push(_config); // eslint-disable-next-line no-plusplus

    index++;
  }

  return result;
}

export { objToYaml, yamlToObj, takeObject, makePostData, ConfigNode };