import _objectDestructuringEmpty from "@babel/runtime/helpers/objectDestructuringEmpty";
import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
var _excluded = ["url"];
/* eslint-disable @atlaskit/design-system/prefer-primitives */
/** @jsx jsx */

import React, { Fragment, useState } from 'react';
import { css, jsx } from '@emotion/react';
import classnames from 'classnames';
import EditorFileIcon from '@atlaskit/icon/glyph/editor/file';
import { useSharedPluginState } from '../../hooks';
import { removeMarginsAndBorder, sharedMultiBodiedExtensionStyles } from '../../ui/MultiBodiedExtension';
import { calculateBreakoutStyles, getExtensionLozengeData } from '../../utils';
import { WithPluginState } from '../../with-plugin-state';
import ExtensionLozenge from '../Extension/Lozenge';
import { useMultiBodiedExtensionActions } from './action-api';
import { mbeExtensionWrapperCSSStyles, overlayStyles } from './styles';
var getContainerCssExtendedStyles = function getContainerCssExtendedStyles(activeChildIndex, showMacroInteractionDesignUpdates) {
  return css(sharedMultiBodiedExtensionStyles.mbeExtensionContainer, _defineProperty({}, ".multiBodiedExtension-content-dom-wrapper > [data-extension-frame='true']:nth-of-type(".concat(activeChildIndex + 1, ")"), css(sharedMultiBodiedExtensionStyles.extensionFrameContent, showMacroInteractionDesignUpdates && removeMarginsAndBorder)));
};
var imageStyles = css({
  maxHeight: '24px',
  maxWidth: '24px'
});
// Similar to the one in platform/packages/editor/editor-common/src/extensibility/Extension/Lozenge.tsx
var getWrapperTitleContent = function getWrapperTitleContent(imageData, title, showMacroInteractionDesignUpdates) {
  if (showMacroInteractionDesignUpdates) {
    return null;
  }
  if (imageData) {
    var url = imageData.url,
      rest = _objectWithoutProperties(imageData, _excluded);
    return jsx("div", {
      className: "extension-title"
    }, jsx("img", _extends({
      css: imageStyles,
      src: url
    }, rest, {
      alt: title
    })), title);
  }
  return jsx("div", {
    className: "extension-title",
    "data-testid": 'multiBodiedExtension-default-lozenge'
  }, jsx(EditorFileIcon, {
    label: title
  }), title);
};
var MultiBodiedExtensionWithWidth = function MultiBodiedExtensionWithWidth(_ref) {
  var node = _ref.node,
    handleContentDOMRef = _ref.handleContentDOMRef,
    getPos = _ref.getPos,
    tryExtensionHandler = _ref.tryExtensionHandler,
    editorView = _ref.editorView,
    eventDispatcher = _ref.eventDispatcher,
    widthState = _ref.widthState,
    editorAppearance = _ref.editorAppearance,
    showMacroInteractionDesignUpdates = _ref.showMacroInteractionDesignUpdates,
    isNodeSelected = _ref.isNodeSelected,
    isNodeHovered = _ref.isNodeHovered,
    isNodeNested = _ref.isNodeNested,
    setIsNodeHovered = _ref.setIsNodeHovered;
  var _node$attrs = node.attrs,
    parameters = _node$attrs.parameters,
    extensionKey = _node$attrs.extensionKey;
  var title = parameters && parameters.extensionTitle || parameters && parameters.macroMetadata && parameters.macroMetadata.title || extensionKey || node.type.name;
  var imageData = getExtensionLozengeData({
    node: node,
    type: 'image'
  });
  var _useState = useState(0),
    _useState2 = _slicedToArray(_useState, 2),
    activeChildIndex = _useState2[0],
    setActiveChildIndex = _useState2[1];
  // Adding to avoid aliasing `this` for the callbacks
  var updateActiveChild = React.useCallback(function (index) {
    if (typeof index !== 'number') {
      setActiveChildIndex(0);
      throw new Error('Index is not valid');
    }
    setActiveChildIndex(index);
    return true;
  }, [setActiveChildIndex]);
  var actions = useMultiBodiedExtensionActions({
    updateActiveChild: updateActiveChild,
    editorView: editorView,
    getPos: getPos,
    eventDispatcher: eventDispatcher,
    node: node
  });
  var extensionHandlerResult = React.useMemo(function () {
    return tryExtensionHandler(actions);
  }, [tryExtensionHandler, actions]);
  var articleRef = React.useCallback(function (node) {
    return handleContentDOMRef(node);
  }, [handleContentDOMRef]);
  var shouldBreakout =
  // Extension should breakout when the layout is set to 'full-width' or 'wide'.
  ['full-width', 'wide'].includes(node.attrs.layout) &&
  // Extension breakout state should not be respected when the editor appearance is full-width mode
  editorAppearance !== 'full-width';
  var mbeWrapperStyles = {};
  if (shouldBreakout) {
    var _calculateBreakoutSty = calculateBreakoutStyles({
        mode: node.attrs.layout,
        widthStateLineLength: widthState === null || widthState === void 0 ? void 0 : widthState.lineLength,
        widthStateWidth: widthState === null || widthState === void 0 ? void 0 : widthState.width
      }),
      breakoutStyles = _extends({}, (_objectDestructuringEmpty(_calculateBreakoutSty), _calculateBreakoutSty));
    mbeWrapperStyles = breakoutStyles;
  }
  var wrapperClassNames = classnames('multiBodiedExtension--wrapper', 'extension-container', 'block', {
    'with-margin-styles': showMacroInteractionDesignUpdates && !isNodeNested,
    'with-border': showMacroInteractionDesignUpdates,
    'with-hover-border': showMacroInteractionDesignUpdates && isNodeHovered,
    'with-danger-overlay': showMacroInteractionDesignUpdates
  });
  var containerClassNames = classnames('multiBodiedExtension--container', {
    'remove-padding': showMacroInteractionDesignUpdates
  });
  var navigationClassNames = classnames('multiBodiedExtension--navigation', {
    'remove-margins': showMacroInteractionDesignUpdates,
    'remove-border': showMacroInteractionDesignUpdates
  });
  var handleMouseEvent = function handleMouseEvent(didHover) {
    if (setIsNodeHovered) {
      setIsNodeHovered(didHover);
    }
  };
  return jsx(Fragment, null, showMacroInteractionDesignUpdates && jsx(ExtensionLozenge, {
    isNodeSelected: isNodeSelected,
    node: node,
    showMacroInteractionDesignUpdates: true,
    customContainerStyles: mbeWrapperStyles,
    isNodeHovered: isNodeHovered
  }), jsx("div", {
    className: wrapperClassNames,
    css: mbeExtensionWrapperCSSStyles,
    "data-testid": "multiBodiedExtension--wrapper",
    style: mbeWrapperStyles,
    onMouseEnter: function onMouseEnter() {
      return handleMouseEvent(true);
    },
    onMouseLeave: function onMouseLeave() {
      return handleMouseEvent(false);
    }
  }, jsx("div", {
    css: overlayStyles,
    className: "multiBodiedExtension--overlay"
  }), getWrapperTitleContent(imageData, title, showMacroInteractionDesignUpdates), jsx("div", {
    className: containerClassNames,
    css: getContainerCssExtendedStyles(activeChildIndex, showMacroInteractionDesignUpdates),
    "data-testid": "multiBodiedExtension--container",
    "data-active-child-index": activeChildIndex
  }, jsx("nav", {
    className: navigationClassNames,
    css: sharedMultiBodiedExtensionStyles.mbeNavigation,
    "data-testid": "multiBodiedExtension-navigation"
  }, extensionHandlerResult), jsx("article", {
    className: "multiBodiedExtension--frames",
    "data-testid": "multiBodiedExtension--frames",
    ref: articleRef
  }))));
};
var MultiBodiedExtensionWithSharedState = function MultiBodiedExtensionWithSharedState(props) {
  var pluginInjectionApi = props.pluginInjectionApi;
  var _useSharedPluginState = useSharedPluginState(pluginInjectionApi, ['width']),
    widthState = _useSharedPluginState.widthState;
  return jsx(MultiBodiedExtensionWithWidth, _extends({
    widthState: widthState
  }, props));
};

// Workaround taken from platform/packages/editor/editor-core/src/plugins/extension/ui/Extension/Extension/index.tsx
var MultiBodiedExtension = function MultiBodiedExtension(props) {
  // TODO: ED-17836 This code is here because confluence injects
  // the `editor-referentiality` plugin via `dangerouslyAppendPlugins`
  // which cannot access the `pluginInjectionApi`. When we move
  // Confluence to using presets we can remove this workaround.
  var pluginInjectionApi = props.pluginInjectionApi;
  return pluginInjectionApi === undefined ? jsx(MultiBodiedExtensionDeprecated, props) : jsx(MultiBodiedExtensionWithSharedState, props);
};

// TODO: ED-17836 This code is here because Confluence injects
// the `editor-referentiality` plugin via `dangerouslyAppendPlugins`
// which cannot access the `pluginInjectionApi`. When we move
// Confluence to using presets we can remove this workaround.
// @ts-ignore
var widthPluginKey = {
  key: 'widthPlugin$',
  getState: function getState(state) {
    return state['widthPlugin$'];
  }
};
var MultiBodiedExtensionDeprecated = function MultiBodiedExtensionDeprecated(props) {
  return (
    // @ts-ignore - 'WithPluginState' cannot be used as a JSX component.
    // This error was introduced after upgrading to TypeScript 5
    jsx(WithPluginState, {
      editorView: props.editorView,
      plugins: {
        widthState: widthPluginKey
      },
      render: function render(_ref2) {
        var widthState = _ref2.widthState;
        return jsx(MultiBodiedExtensionWithWidth, _extends({
          widthState: widthState
        }, props));
      }
    })
  );
};
/**
 * End workaround
 */

export default MultiBodiedExtension;