import _extends from "@babel/runtime/helpers/extends";
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 const 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'],
  [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'],
  [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'],
  [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 const 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;
  }
};
const isActionGroup = node => /*#__PURE__*/React.isValidElement(node) && node.type === ActionGroup;
const isElementDisplayValid = (name, display) => {
  var _ElementDisplaySchema, _ElementDisplaySchema2;
  return (_ElementDisplaySchema = (_ElementDisplaySchema2 = ElementDisplaySchema[name]) === null || _ElementDisplaySchema2 === void 0 ? void 0 : _ElementDisplaySchema2.includes(display)) !== null && _ElementDisplaySchema !== void 0 ? _ElementDisplaySchema : false;
};
export const isJSXElementNull = children => {
  return Boolean(children.type() === null);
};
const isElementOrElementGroup = node => /*#__PURE__*/React.isValidElement(node) && (isFlexibleUiElement(node) || node.type === ElementGroup);
export const filterActionItems = (items = [], context, product) => {
  return items.filter(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 const renderChildren = (children, size) => React.Children.map(children, child => {
  if (isElementOrElementGroup(child) || isActionGroup(child)) {
    const node = child;
    const {
      size: childSize
    } = node.props;
    return /*#__PURE__*/React.cloneElement(node, {
      size: childSize || size
    });
  }
  return child;
});
export const renderElementItems = (items = [], display = 'inline') => {
  const elements = items.reduce((acc, curr, idx) => {
    const {
      name,
      ...props
    } = curr;
    const Element = Elements[name];
    const typedProps = props;
    if (Element && isElementDisplayValid(name, display)) {
      const element = /*#__PURE__*/React.createElement(Element, _extends({
        key: idx
      }, typedProps));
      if (!isJSXElementNull(element)) {
        return [...acc, element];
      }
    }
    return acc;
  }, []);
  if (elements.length) {
    return elements;
  }
};