text: "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.maybeSerializeMenuItem = maybeSerializeMenuItem;
  exports.getOrderingMenuItem = getOrderingMenuItem;
  exports.getOrderingMenuItemsForSchemaType = getOrderingMenuItemsForSchemaType;
  exports.MenuItemBuilder = void 0;
  
  var _getExtendedProjection = require("./util/getExtendedProjection");
  
  var _Schema = require("./parts/Schema");
  
  var _Icon = require("./parts/Icon");
  
  var _Sort = require("./Sort");
  
  var _SerializeError = require("./SerializeError");
  
  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) { _defineProperty(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; }
  
  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; }
  
  var SortIcon = (0, _Icon.getSortIcon)();
  
  function maybeSerializeMenuItem(item, index, path) {
    return item instanceof MenuItemBuilder ? item.serialize({
      path,
      index
    }) : item;
  }
  
  class MenuItemBuilder {
    constructor(spec) {
      _defineProperty(this, "spec", void 0);
  
      this.spec = spec ? spec : {};
    }
  
    action(action) {
      return this.clone({
        action
      });
    }
  
    getAction() {
      return this.spec.action;
    }
  
    intent(intent) {
      return this.clone({
        intent
      });
    }
  
    getIntent() {
      return this.spec.intent;
    }
  
    title(title) {
      return this.clone({
        title
      });
    }
  
    getTitle() {
      return this.spec.title;
    }
  
    group(group) {
      return this.clone({
        group
      });
    }
  
    getGroup() {
      return this.spec.group;
    }
  
    icon(icon) {
      return this.clone({
        icon
      });
    }
  
    getIcon() {
      return this.spec.icon;
    }
  
    params(params) {
      return this.clone({
        params
      });
    }
  
    getParams() {
      return this.spec.params;
    }
  
    showAsAction(showAsAction) {
      return this.clone({
        showAsAction
      });
    }
  
    getShowAsAction() {
      return this.spec.showAsAction;
    }
  
    serialize() {
      var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
        path: []
      };
      var _this$spec = this.spec,
          title = _this$spec.title,
          action = _this$spec.action,
          intent = _this$spec.intent;
  
      if (!title) {
        var hint = typeof action === 'string' ? "action: \"".concat(action, "\"") : undefined;
        throw new _SerializeError.SerializeError('`title` is required for menu item', options.path, options.index, hint).withHelpUrl(_SerializeError.HELP_URL.TITLE_REQUIRED);
      }
  
      if (!action && !intent) {
        throw new _SerializeError.SerializeError("`action` or `intent` required for menu item with title ".concat(this.spec.title), options.path, options.index, "\"".concat(title, "\"")).withHelpUrl(_SerializeError.HELP_URL.ACTION_OR_INTENT_REQUIRED);
      }
  
      if (intent && action) {
        throw new _SerializeError.SerializeError('cannot set both `action` AND `intent`', options.path, options.index, "\"".concat(title, "\"")).withHelpUrl(_SerializeError.HELP_URL.ACTION_AND_INTENT_MUTUALLY_EXCLUSIVE);
      }
  
      return _objectSpread(_objectSpread({}, this.spec), {}, {
        title
      });
    }
  
    clone(withSpec) {
      var builder = new MenuItemBuilder();
      builder.spec = _objectSpread(_objectSpread({}, this.spec), withSpec || {});
      return builder;
    }
  
  }
  
  exports.MenuItemBuilder = MenuItemBuilder;
  
  function getOrderingMenuItem(ordering, extendedProjection) {
    return new MenuItemBuilder().group('sorting').title("Sort by ".concat(ordering.title)).icon(SortIcon).action('setSortOrder').params({
      by: ordering.by,
      extendedProjection
    });
  }
  
  function getOrderingMenuItemsForSchemaType(typeName) {
    var type = typeof typeName === 'string' ? (0, _Schema.getDefaultSchema)().get(typeName) : typeName;
  
    if (!type) {
      return [];
    }
  
    return (type.orderings ? type.orderings.concat(_Sort.DEFAULT_ORDERING_OPTIONS) : _Sort.DEFAULT_ORDERING_OPTIONS).map(ordering => getOrderingMenuItem(ordering, (0, _getExtendedProjection.getExtendedProjection)(type, ordering.by)));
  }