"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.renderElementItems = exports.renderChildren = exports.isJSXElementNull = exports.getGapSize = exports.filterActionItems = exports.ElementDisplaySchema = void 0;
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _react = _interopRequireDefault(require("react"));
var _constants = require("../../../../constants");
var _useBlockCardRovoActionExperiment = require("../../../../state/hooks/use-block-card-rovo-action-experiment");
var _flexible = require("../../../../utils/flexible");
var Elements = _interopRequireWildcard(require("../elements"));
var _actionGroup = _interopRequireDefault(require("./action-group"));
var _elementGroup = _interopRequireDefault(require("./element-group"));
var _excluded = ["name"];
var _ElementDisplaySchema;
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); }
// Determine whether the element can be display as inline/block.

var ElementDisplaySchema = exports.ElementDisplaySchema = (_ElementDisplaySchema = {}, (0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)(_ElementDisplaySchema, _constants.ElementName.AssignedTo, ['inline']), _constants.ElementName.AssignedToGroup, ['inline']), _constants.ElementName.AttachmentCount, ['inline']), _constants.ElementName.AuthorGroup, ['inline']), _constants.ElementName.AppliedToComponentsCount, ['inline']), _constants.ElementName.ChecklistProgress, ['inline']), _constants.ElementName.CollaboratorGroup, ['inline']), _constants.ElementName.CommentCount, ['inline']), _constants.ElementName.CreatedBy, ['inline']), _constants.ElementName.CreatedOn, ['inline']), (0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)(_ElementDisplaySchema, _constants.ElementName.DueOn, ['inline']), _constants.ElementName.LatestCommit, ['inline']), _constants.ElementName.LinkIcon, ['inline']), _constants.ElementName.Location, ['inline']), _constants.ElementName.ModifiedBy, ['inline']), _constants.ElementName.ModifiedOn, ['inline']), _constants.ElementName.OwnedBy, ['inline']), _constants.ElementName.OwnedByGroup, ['inline']), _constants.ElementName.Preview, ['block']), _constants.ElementName.Priority, ['inline']), (0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)(_ElementDisplaySchema, _constants.ElementName.ProgrammingLanguage, ['inline']), _constants.ElementName.Provider, ['inline']), _constants.ElementName.ReactCount, ['inline']), _constants.ElementName.ReadTime, ['inline']), _constants.ElementName.Snippet, ['block']), _constants.ElementName.SourceBranch, ['inline']), _constants.ElementName.SentOn, ['inline']), _constants.ElementName.State, ['inline']), _constants.ElementName.SubscriberCount, ['inline']), _constants.ElementName.TeamMemberCount, ['inline']), (0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)((0, _defineProperty2.default)(_ElementDisplaySchema, _constants.ElementName.SubTasksProgress, ['inline']), _constants.ElementName.StoryPoints, ['inline']), _constants.ElementName.TargetBranch, ['inline']), _constants.ElementName.Title, ['inline']), _constants.ElementName.UserAttributes, ['inline']), _constants.ElementName.ViewCount, ['inline']), _constants.ElementName.VoteCount, ['inline']), _constants.ElementName.HostName, ['inline']));

/**
 * Get gap size between elements inside a block
 * Equivalent version for DS primitives space token is getPrimitivesInlineSpaceBySize()
 * at view/FlexibleCard/components/utils.tsx
 */
var getGapSize = exports.getGapSize = function getGapSize(size) {
  switch (size) {
    case _constants.SmartLinkSize.XLarge:
      return 1.25;
    case _constants.SmartLinkSize.Large:
      return 1;
    case _constants.SmartLinkSize.Medium:
      return 0.5;
    case _constants.SmartLinkSize.Small:
    default:
      return 0.25;
  }
};
var isActionGroup = function isActionGroup(node) {
  return /*#__PURE__*/_react.default.isValidElement(node) && node.type === _actionGroup.default;
};
var isElementDisplayValid = function isElementDisplayValid(name, display) {
  var _ElementDisplaySchema2, _ElementDisplaySchema3;
  return (_ElementDisplaySchema2 = (_ElementDisplaySchema3 = ElementDisplaySchema[name]) === null || _ElementDisplaySchema3 === void 0 ? void 0 : _ElementDisplaySchema3.includes(display)) !== null && _ElementDisplaySchema2 !== void 0 ? _ElementDisplaySchema2 : false;
};
var isJSXElementNull = exports.isJSXElementNull = function isJSXElementNull(children) {
  return Boolean(children.type() === null);
};
var isElementOrElementGroup = function isElementOrElementGroup(node) {
  return /*#__PURE__*/_react.default.isValidElement(node) && ((0, _flexible.isFlexibleUiElement)(node) || node.type === _elementGroup.default);
};
var filterActionItems = exports.filterActionItems = function filterActionItems() {
  var items = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var context = arguments.length > 1 ? arguments[1] : undefined;
  var product = arguments.length > 2 ? arguments[2] : undefined;
  var isBlockCard3PExperimentEnabled = (0, _useBlockCardRovoActionExperiment.isBlockCardRovoActionExperimentEnabled)(product);
  return items.filter(function (item) {
    switch (item.name) {
      case _constants.ActionName.DeleteAction:
      case _constants.ActionName.EditAction:
      case _constants.ActionName.CustomAction:
        // Named and custom actions that user defines.
        return Boolean(_constants.ActionName[item.name]);
      case _constants.ActionName.RovoChatAction:
        if (isBlockCard3PExperimentEnabled) {
          return Boolean(_constants.ActionName[item.name]);
        }
        // same as default case below
        // remove on cleanup of platform_sl_3p_auth_rovo_block_card_kill_switch
        if ((context === null || context === void 0 ? void 0 : context.actions) === undefined) {
          return false;
        }
        return Boolean(item.name in context.actions ? context.actions[item.name] : undefined);
      default:
        // Action that require data from the data context to render.
        if ((context === null || context === void 0 ? void 0 : context.actions) === undefined) {
          return false;
        }
        return Boolean(item.name in context.actions ? context.actions[item.name] : undefined);
    }
  });
};
var renderChildren = exports.renderChildren = function renderChildren(children, size) {
  return _react.default.Children.map(children, function (child) {
    if (isElementOrElementGroup(child) || isActionGroup(child)) {
      var node = child;
      var childSize = node.props.size;
      return /*#__PURE__*/_react.default.cloneElement(node, {
        size: childSize || size
      });
    }
    return child;
  });
};
var renderElementItems = exports.renderElementItems = function renderElementItems() {
  var items = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
  var display = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'inline';
  var elements = items.reduce(function (acc, curr, idx) {
    var name = curr.name,
      props = (0, _objectWithoutProperties2.default)(curr, _excluded);
    var Element = Elements[name];
    var typedProps = props;
    if (Element && isElementDisplayValid(name, display)) {
      var element = /*#__PURE__*/_react.default.createElement(Element, (0, _extends2.default)({
        key: idx
      }, typedProps));
      if (!isJSXElementNull(element)) {
        return [].concat((0, _toConsumableArray2.default)(acc), [element]);
      }
    }
    return acc;
  }, []);
  if (elements.length) {
    return elements;
  }
};