"use strict";

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

var _util = require("./util");

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

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(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

/**
 * manage dataSource for menu list
 */
var DataStore =
/*#__PURE__*/
function () {
  function DataStore(options) {
    _classCallCheck(this, DataStore);

    this.options = _objectSpread({
      filter: _util.filter,
      key: undefined,
      addonKey: false,
      filterLocal: true
    }, options); // origin data

    this.dataSource = []; // current data for menu display

    this.menuDataSource = []; // key=>value map for menuDataSource

    this.mapDataSource = {}; // current data can be select (not disabled) on menu

    this.enabledDataSource = [];
    this.flattenDataSource = [];
  }

  _createClass(DataStore, [{
    key: "setOptions",
    value: function setOptions(options) {
      _extends(this.options, options);
    }
  }, {
    key: "updateByDS",
    value: function updateByDS(dataSource) {
      var isChildren = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
      this.dataSource = isChildren ? (0, _util.parseDataSourceFromChildren)(dataSource) : (0, _util.normalizeDataSource)(dataSource);
      return this.updateAll();
    }
  }, {
    key: "updateByKey",
    value: function updateByKey(key) {
      if (key === this.options.key) {
        return this.getMenuDS();
      }

      this.options.key = key;
      return this.updateAll();
    }
  }, {
    key: "getOriginDS",
    value: function getOriginDS() {
      return this.dataSource;
    }
  }, {
    key: "getMenuDS",
    value: function getMenuDS() {
      return this.menuDataSource;
    }
  }, {
    key: "getFlattenDS",
    value: function getFlattenDS() {
      return this.flattenDataSource;
    }
  }, {
    key: "getEnableDS",
    value: function getEnableDS() {
      return this.enabledDataSource;
    }
  }, {
    key: "getMapDS",
    value: function getMapDS() {
      return this.mapDataSource;
    }
  }, {
    key: "updateAll",
    value: function updateAll() {
      var _this = this;

      var _this$options = this.options,
          key = _this$options.key,
          filter = _this$options.filter,
          filterLocal = _this$options.filterLocal;
      this.menuDataSource = (0, _util.filterDataSource)(this.dataSource, filterLocal ? key : '', filter, this.options.addonKey);
      this.flattenDataSource = (0, _util.flattingDataSource)(this.menuDataSource);
      this.mapDataSource = {};
      this.flattenDataSource.forEach(function (item) {
        _this.mapDataSource["".concat(item.value)] = item;
      });
      this.enabledDataSource = this.flattenDataSource.filter(function (item) {
        return !item.disabled;
      });
      return this.menuDataSource;
    }
  }]);

  return DataStore;
}();

var _default = DataStore;
exports["default"] = _default;