import { Plugin } from '@atlaskit/editor-prosemirror/state';
import { fg } from '@atlaskit/platform-feature-flags';
import { expValEquals } from '@atlaskit/tmp-editor-statsig/exp-val-equals';
import { isSSR } from '../core-utils/is-ssr';
import { getNodeIdProvider } from '../node-anchor/node-anchor-provider';
import { createProseMirrorMetadata } from '../prosemirror-dom-metadata';
import { ANCHOR_VARIABLE_NAME, isCSSAnchorSupported, isCSSAttrAnchorSupported } from '../styles/shared/native-anchor';
/**
 * 🧱 Internal Helper Function: Editor FE Platform
 *
 * Attaches generic ProseMirror metadata attributes to a given DOM element based on the properties of a ProseMirror node.
 * This function is useful for annotating DOM elements with metadata that describes the type and characteristics of the ProseMirror node.
 *
 *
 * @param {object} params - The parameters for the function.
 * @param {PMNode} params.node - The ProseMirror node from which to derive metadata.
 * @param {HTMLElement} params.dom - The DOM element to which the metadata attributes will be attached.
 */
export const attachGenericProseMirrorMetadata = ({
  nodeOrMark,
  dom,
  options
}) => {
  const metadata = createProseMirrorMetadata(nodeOrMark, options);
  Object.entries(metadata).forEach(([name, value]) => {
    dom.setAttribute(name, value);
    if (name === 'data-node-anchor' && expValEquals('platform_editor_native_anchor_with_dnd', 'isEnabled', true)) {
      // if browser doesn't support CSS anchor, won't need the style
      // Or if it supports CSS attr() function as the value of anchor-name,
      // We won't need set the style
      if (!isCSSAnchorSupported() || isCSSAttrAnchorSupported()) {
        return;
      }

      // otherwise, we set the CSS variable for anchor-name
      dom.style.setProperty(ANCHOR_VARIABLE_NAME, `${value}`);
      return;
    }
  });
};

/** Type guard to check if a Node is an HTMLElement in a safe way. */
const isHTMLElement = element => {
  if (element === null || element === undefined) {
    return false;
  }

  // In SSR `HTMLElement` is not defined, so we need to use duck typing here
  return 'innerHTML' in element && 'style' in element && 'classList' in element;
};

// Wraper to avoid any exception during the get pos operation
// See this https://hello.atlassian.net/wiki/spaces/EDITOR/pages/2849713193/ED-19672+Extensions+Regression
// And this https://discuss.prosemirror.net/t/possible-bug-on-viewdesc-posbeforechild/5783
const wrapGetPosExceptions = spec => {
  var _spec$props;
  if (!(spec !== null && spec !== void 0 && (_spec$props = spec.props) !== null && _spec$props !== void 0 && _spec$props.nodeViews)) {
    return spec;
  }
  const unsafeNodeViews = spec.props.nodeViews;
  let nodeIdProvider;
  const safeNodeViews = new Proxy(unsafeNodeViews, {
    get(target, prop, receiver) {
      const safeNodeView = new Proxy(Reflect.get(target, prop, receiver), {
        apply(target, thisArg, argumentsList) {
          const [node, view, unsafeGetPos, ...more] = argumentsList;
          if (!nodeIdProvider && expValEquals('platform_editor_native_anchor_with_dnd', 'isEnabled', true)) {
            nodeIdProvider = getNodeIdProvider(view);
          }
          const safeGetPos = (() => {
            try {
              return unsafeGetPos();
            } catch (e) {
              return;
            }

            // eslint-disable-next-line no-extra-bind
          }).bind(thisArg);
          const result = Reflect.apply(target, thisArg, [node, view, safeGetPos, ...more]);
          if ((result === null || result === void 0 ? void 0 : result.dom) instanceof HTMLElement ||
          // SSR result?.dom is not an instance of HTMLElement, but we still want to attach metadata to it
          isSSR() && isHTMLElement(result === null || result === void 0 ? void 0 : result.dom) && fg('platform_editor_native_anchor_patch_3')) {
            var _nodeIdProvider;
            // we only attach metadata to the dom if its position is known
            const pos = safeGetPos();
            const options = pos !== undefined && expValEquals('platform_editor_native_anchor_with_dnd', 'isEnabled', true) ? {
              anchrorId: (_nodeIdProvider = nodeIdProvider) === null || _nodeIdProvider === void 0 ? void 0 : _nodeIdProvider.getOrGenerateId(node, pos)
            } : undefined;
            attachGenericProseMirrorMetadata({
              nodeOrMark: node,
              dom: result.dom,
              options
            });
          }
          return result;
        }
      });
      return safeNodeView;
    }
  });
  spec.props.nodeViews = safeNodeViews;
  return spec;
};

// Ignored via go/ees005
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export class SafePlugin extends Plugin {
  // This variable isn't (and shouldn't) be used anywhere. Its purpose is
  // to distinguish Plugin from SafePlugin, thus ensuring that an 'unsafe'
  // Plugin cannot be assigned as an item in EditorPlugin → pmPlugins.

  constructor(spec) {
    super(wrapGetPosExceptions(spec));
  }
}