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 storedMarks = tr.storedMarks || state.storedMarks || selection.$cursor && selection.$cursor.marks && selection.$cursor.marks() || [];
    var sm = storedMarks.find(function (m) {
      return m.type === markType;
    });
    return sm && sm.attrs.name || FONT_TYPE_NAME_DEFAULT;
  }
  var mark = findActiveMark(doc, from, to, markType);
  var fontName = mark && mark.attrs.name;
  if (!fontName) {
    return FONT_TYPE_NAME_DEFAULT;
  }
  return fontName;
}