import { Extension, textInputRule } from '@tiptap/core'

// Typography adds the small print-shop niceties as you type: curly quotes,
// em-dashes, and a single-character ellipsis. Implemented as input rules (no
// dependency, offline-friendly — matching FontSize.ts's hand-rolled approach),
// and every one is plain Undo-able if it ever fires when you didn't want it.
//
// CJK is untouched: these rules only rewrite ASCII quotes/dashes/dots, never the
// fullwidth forms a Mandarin sentence uses.
export const Typography = Extension.create({
  name: 'petalTypography',

  addInputRules() {
    return [
      // “ opening double quote — after start, whitespace, or an opening bracket.
      textInputRule({ find: /(?:^|[\s{[(<'"‘“])(")$/, replace: '“' }),
      // ” closing double quote — any other time you type a ".
      textInputRule({ find: /"$/, replace: '”' }),
      // ‘ opening single quote.
      textInputRule({ find: /(?:^|[\s{[(<'"‘“])(')$/, replace: '‘' }),
      // ’ closing single quote / apostrophe.
      textInputRule({ find: /'$/, replace: '’' }),
      // — em dash from a double hyphen.
      textInputRule({ find: /--$/, replace: '—' }),
      // … ellipsis from three dots.
      textInputRule({ find: /\.\.\.$/, replace: '…' }),
    ]
  },
})
