"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.resolved = exports.resolveColumn = exports.isValidValue = exports.isColumn = exports.isCascader = exports.resolvedFieldNames = void 0;

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _omit = _interopRequireDefault(require("lodash/omit"));

var _utils = require("../utils");

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { (0, _defineProperty2.default)(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

var DEFAULT_FIELD_NAMES = {
  value: 'value',
  label: 'label',
  children: 'children'
};

var resolvedFieldNames = function resolvedFieldNames(left) {
  var merged = _objectSpread({}, DEFAULT_FIELD_NAMES);

  left && Object.keys(left).forEach(function (key) {
    merged[key] = (left === null || left === void 0 ? void 0 : left[key]) || DEFAULT_FIELD_NAMES[key];
  });
  return merged;
};

exports.resolvedFieldNames = resolvedFieldNames;

var isCascader = function isCascader(dataSource) {
  return Array.isArray(dataSource) && dataSource[0] && !Array.isArray(dataSource[0]);
};

exports.isCascader = isCascader;

var isColumn = function isColumn(dataSource) {
  return Array.isArray(dataSource) && dataSource[0] && Array.isArray(dataSource[0]);
};

exports.isColumn = isColumn;

var isValidValue = function isValidValue(value) {
  var currentValue = (0, _utils.toArray)(value);
  return currentValue.some(function (item) {
    return !!item || item === 0 || item === false;
  });
};

exports.isValidValue = isValidValue;

var resolvedValue = function resolvedValue(props, initialValue) {
  var value = props.value,
      defaultValue = props.defaultValue,
      wheelDefaultValue = props.wheelDefaultValue;

  if ('value' in props && isValidValue(value)) {
    return (0, _utils.toArray)(value);
  }

  if ('defaultValue' in props && isValidValue(defaultValue)) {
    return (0, _utils.toArray)(defaultValue);
  }

  if ('wheelDefaultValue' in props && isValidValue(wheelDefaultValue)) {
    return (0, _utils.toArray)(wheelDefaultValue);
  }

  return (0, _utils.toArray)(initialValue);
};

var resolveColumn = function resolveColumn(props) {
  var columns = (0, _utils.toArray)(props.dataSource);
  var fieldNames = resolvedFieldNames(props.fieldNames);
  var value = resolvedValue(props, columns.map(function (item) {
    var _item$;

    return item === null || item === void 0 ? void 0 : (_item$ = item[0]) === null || _item$ === void 0 ? void 0 : _item$[fieldNames === null || fieldNames === void 0 ? void 0 : fieldNames.value];
  }));
  return {
    value: value,
    columns: columns,
    items: columns.map(function (column, index) {
      return column.filter(function (option) {
        return (option === null || option === void 0 ? void 0 : option[fieldNames.value]) === value[index];
      })[0];
    })
  };
};

exports.resolveColumn = resolveColumn;

var resolveCascade = function resolveCascade(props) {
  var cols = props.cols;
  var fieldNames = resolvedFieldNames(props.fieldNames);
  var value = resolvedValue(props, []);
  var columns = [];
  var items = [];

  var traverse = function traverse(options) {
    var depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
    columns[depth] = options.map(function (option, index) {
      var rest = (0, _omit.default)(option, fieldNames.children);
      var children = option[fieldNames.children];
      var currentValue = value[depth];

      if (isValidValue(currentValue) && rest[fieldNames.value] === currentValue || !isValidValue(currentValue) && index === 0) {
        value[depth] = rest[fieldNames.value];
        items[depth] = rest;

        if (Array.isArray(children) && children.length > 0 && depth + 1 < cols) {
          traverse(children, depth + 1);
        }
      }

      return rest;
    });
  };

  traverse(props.dataSource);
  return {
    value: value,
    items: items,
    columns: columns
  };
};

var resolved = function resolved(props) {
  return isCascader(props.dataSource) ? resolveCascade(props) : resolveColumn(props);
};

exports.resolved = resolved;