import { EditorState } from 'prosemirror-state';
import { MARK_FONT_TYPE } from '../MarkNames.js';
import findActiveMark from '../findActiveMark.js';

// This should map to `--czi-content-font-size` at `czi-editor.css`.
export var FONT_TYPE_NAME_DEFAULT = 'Arial';
export default function findActiveFontType(state) {
  var schema = state.schema,
    doc = state.doc,
    selection = state.selection,
    tr = state.tr;
  var markType = schema.marks[MARK_FONT_TYPE];
  if (!markType) {
    return FONT_TYPE_NAME_DEFAULT;
  }
  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 === null || sm === void 0 ? void 0 : sm.attrs.name) || FONT_TYPE_NAME_DEFAULT;
  }
  var mark = findActiveMark(doc, from, to, markType);
  var fontName = mark === null || mark === void 0 ? void 0 : mark.attrs.name;
  if (!fontName) {
    return FONT_TYPE_NAME_DEFAULT;
  }
  return fontName;
}