"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.buildSelectedData = buildSelectedData;
exports.buildBreadcrumbData = buildBreadcrumbData;
exports.buildSelectedCodes = buildSelectedCodes;
exports.getSearchResult = getSearchResult;

var _last = _interopRequireDefault(require("lodash/last"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread 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 _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

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

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; }

/** *******************Select************************** */

/** *******************Panel************************** */
var loop = function loop(data, code, callback) {
  data.forEach(function (item, index, arr) {
    if (item.code === code) {
      return callback(item, index, arr);
    }

    if (item.categories) {
      return loop(item.categories, code, callback);
    }

    return null;
  });
};

function buildSelectedData(data, selectedCodes) {
  if (!data || !data.code) {
    return {};
  }

  if (!selectedCodes || !selectedCodes.length) {
    return data;
  }

  var selectedCode = (0, _last["default"])(selectedCodes);
  var selectedData = null;
  loop([data], selectedCode, function (item) {
    selectedData = item;
  });
  return selectedData;
}

function buildBreadcrumbData(data, selectedCodes) {
  if (!data || !data.code) {
    return null;
  }

  var codes = [data.code].concat(_toConsumableArray(selectedCodes));
  var breadcrumbData = [];
  codes.forEach(function (code) {
    loop([data], code, function (item) {
      breadcrumbData.push({
        code: item.code,
        name: item.name
      });
    });
  });
  return breadcrumbData;
}

function buildSelectedCodes(selectedCodes, code) {
  var index = selectedCodes.indexOf(code);
  return selectedCodes.slice(0, index + 1);
}

function getSearchResult(data) {
  return data ? data.map(function (item) {
    return {
      code: item.code,
      name: item.name,
      fullName: item.fullName
    };
  }) : [];
}
//# sourceMappingURL=util.js.map
