"use strict";

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

var _utils = require("../picker-view/utils");

var parseState = function parseState(props) {
  var value = props.value,
      defaultValue = props.defaultValue;
  var currentValue = [];
  return {
    value: value || defaultValue,
    currentValue: currentValue
  };
};

exports.parseState = parseState;

var parseItems = function parseItems(options, value, fieldNames) {
  var field = (0, _utils.resolvedFieldNames)(fieldNames);
  var items = [];

  var traverse = function traverse(memo) {
    var depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
    memo.forEach(function (option) {
      var children = option[field.children];
      var currentValue = value[depth];

      if (option[field.value] === currentValue) {
        items[depth] = option;

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

  traverse(options);
  return items;
};

exports.parseItems = parseItems;