import { EditorState } from 'prosemirror-state';
import { findParentNodeOfType } from 'prosemirror-utils';
import { MARK_FONT_SIZE } from '../MarkNames.js';
import { HEADING } from '../NodeNames.js';
import findActiveMark from '../findActiveMark.js';

// This should map to `--czi-content-font-size` at `czi-editor.css`.
var FONT_PT_SIZE_DEFAULT = 11;

// This should map to `czi-heading.css`.
var MAP_HEADING_LEVEL_TO_FONT_PT_SIZE = {
  '1': 20,
  '2': 18,
  '3': 16,
  '4': 14,
  '5': 11,
  '6': 11
};
export default function findActiveFontSize(state) {
  var schema = state.schema,
    doc = state.doc,
    selection = state.selection,
    tr = state.tr;
  var markType = schema.marks[MARK_FONT_SIZE];
  var heading = schema.nodes[HEADING];
  var defaultSize = String(FONT_PT_SIZE_DEFAULT);
  if (!markType) {
    return defaultSize;
  }
  var from = selection.from,
    to = selection.to,
    empty = selection.empty;
  if (empty) {
    var _selection$$cursor, _selection$$cursor$ma;
    var storedMarks = tr.storedMarks || state.storedMarks || ((_selection$$cursor = selection.$cursor) === null || _selection$$cursor === void 0 || (_selection$$cursor$ma = _selection$$cursor.marks) === null || _selection$$cursor$ma === void 0 ? void 0 : _selection$$cursor$ma.call(_selection$$cursor)) || [];
    var sm = storedMarks.find(function (m) {
      return m.type === markType;
    });
    return sm ? String(sm.attrs.pt || defaultSize) : defaultSize;
  }
  var mark = findActiveMark(doc, from, to, markType);
  if (mark) {
    return String(mark.attrs.pt);
  }
  if (!heading) {
    return defaultSize;
  }
  var result = findParentNodeOfType(heading)(state.selection);
  if (!result) {
    return defaultSize;
  }
  var level = String(result.node.attrs.level);
  return MAP_HEADING_LEVEL_TO_FONT_PT_SIZE[level] || defaultSize;
}