import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var _excluded = ["name"];
var _ElementDisplaySchema;
import React from 'react';
import { ActionName, ElementName, SmartLinkSize } from '../../../../constants';
import { isFlexibleUiElement } from '../../../../utils/flexible';
import * as Elements from '../elements';
import ActionGroup from './action-group';
import ElementGroup from './element-group';

// Determine whether the element can be display as inline/block.

export var ElementDisplaySchema = (_ElementDisplaySchema = {}, _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_ElementDisplaySchema, ElementName.AssignedTo, ['inline']), ElementName.AssignedToGroup, ['inline']), ElementName.AttachmentCount, ['inline']), ElementName.AuthorGroup, ['inline']), ElementName.AppliedToComponentsCount, ['inline']), ElementName.ChecklistProgress, ['inline']), ElementName.CollaboratorGroup, ['inline']), ElementName.CommentCount, ['inline']), ElementName.CreatedBy, ['inline']), ElementName.CreatedOn, ['inline']), _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_ElementDisplaySchema, ElementName.DueOn, ['inline']), ElementName.LatestCommit, ['inline']), ElementName.LinkIcon, ['inline']), ElementName.Location, ['inline']), ElementName.ModifiedBy, ['inline']), ElementName.ModifiedOn, ['inline']), ElementName.OwnedBy, ['inline']), ElementName.OwnedByGroup, ['inline']), ElementName.Preview, ['block']), ElementName.Priority, ['inline']), _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_ElementDisplaySchema, ElementName.ProgrammingLanguage, ['inline']), ElementName.Provider, ['inline']), ElementName.ReactCount, ['inline']), ElementName.ReadTime, ['inline']), ElementName.Snippet, ['block']), ElementName.SourceBranch, ['inline']), ElementName.SentOn, ['inline']), ElementName.State, ['inline']), ElementName.SubscriberCount, ['inline']), ElementName.TeamMemberCount, ['inline']), _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty(_ElementDisplaySchema, ElementName.SubTasksProgress, ['inline']), ElementName.StoryPoints, ['inline']), ElementName.TargetBranch, ['inline']), ElementName.Title, ['inline']), ElementName.UserAttributes, ['inline']), ElementName.ViewCount, ['inline']), ElementName.VoteCount, ['inline']), 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
 */
export var getGapSize = function getGapSize(size) {
  switch (size) {
    case SmartLinkSize.XLarge:
      return 1.25;
    case SmartLinkSize.Large:
      return 1;
    case SmartLinkSize.Medium:
      return 0.5;
    case SmartLinkSize.Small:
    default:
      return 0.25;
  }
};
var isActionGroup = function isActionGroup(node) {
  return /*#__PURE__*/React.isValidElement(node) && node.type === ActionGroup;
};
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;
};
export var isJSXElementNull = function isJSXElementNull(children) {
  return Boolean(children.type() === null);
};
var isElementOrElementGroup = function isElementOrElementGroup(node) {
  return /*#__PURE__*/React.isValidElement(node) && (isFlexibleUiElement(node) || node.type === ElementGroup);
};
export var 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;
  return items.filter(function (item) {
    switch (item.name) {
      case ActionName.DeleteAction:
      case ActionName.EditAction:
      case ActionName.CustomAction:
        // Named and custom actions that user defines.
        return Boolean(ActionName[item.name]);
      case ActionName.RovoChatAction:
        if (!!product && product === 'CONFLUENCE') {
          return Boolean(ActionName[item.name]);
        }
        // same as default case below
        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);
    }
  });
};
export var renderChildren = function renderChildren(children, size) {
  return React.Children.map(children, function (child) {
    if (isElementOrElementGroup(child) || isActionGroup(child)) {
      var node = child;
      var childSize = node.props.size;
      return /*#__PURE__*/React.cloneElement(node, {
        size: childSize || size
      });
    }
    return child;
  });
};
export var 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 = _objectWithoutProperties(curr, _excluded);
    var Element = Elements[name];
    var typedProps = props;
    if (Element && isElementDisplayValid(name, display)) {
      var element = /*#__PURE__*/React.createElement(Element, _extends({
        key: idx
      }, typedProps));
      if (!isJSXElementNull(element)) {
        return [].concat(_toConsumableArray(acc), [element]);
      }
    }
    return acc;
  }, []);
  if (elements.length) {
    return elements;
  }
};