{"version":3,"file":"use-mentions-history.mjs","names":[],"sources":["../../../src/components/Mentions/use-mentions-history.ts"],"sourcesContent":["import { useRef } from 'react';\nimport type { MentionsChangeReasonType } from './Mentions.types';\n\nexport type MentionsHistoryChangeType =\n  | Exclude<MentionsChangeReasonType, 'undo' | 'redo'>\n  | 'external';\n\ninterface MentionsHistoryEntry {\n  value: string;\n  caret: number;\n  caretBefore: number;\n}\n\nexport interface MentionsHistoryRestore {\n  value: string;\n  caret: number;\n}\n\ninterface MentionsHistoryState {\n  entries: MentionsHistoryEntry[];\n  index: number;\n  lastType: MentionsHistoryChangeType | null;\n  lastTime: number;\n}\n\nconst MAX_ENTRIES = 100;\nconst COALESCE_MS = 400;\n\nexport function useMentionsHistory(initialValue: string, initialCaret: number) {\n  const ref = useRef<MentionsHistoryState>(null as unknown as MentionsHistoryState);\n\n  if (ref.current === null) {\n    ref.current = {\n      entries: [{ value: initialValue, caret: initialCaret, caretBefore: initialCaret }],\n      index: 0,\n      lastType: null,\n      lastTime: 0,\n    };\n  }\n\n  const push = (\n    value: string,\n    caret: number,\n    caretBefore: number,\n    type: MentionsHistoryChangeType\n  ) => {\n    const history = ref.current;\n    const now = Date.now();\n    const current = history.entries[history.index];\n    if (current.value === value) {\n      return;\n    }\n\n    const shouldCoalesce =\n      type === 'input' && history.lastType === 'input' && now - history.lastTime < COALESCE_MS;\n\n    if (shouldCoalesce) {\n      history.entries[history.index] = { value, caret, caretBefore: current.caretBefore };\n    } else {\n      history.entries = history.entries.slice(0, history.index + 1);\n      history.entries.push({ value, caret, caretBefore });\n      if (history.entries.length > MAX_ENTRIES) {\n        history.entries.shift();\n      }\n      history.index = history.entries.length - 1;\n    }\n\n    history.lastType = type;\n    history.lastTime = now;\n  };\n\n  const undo = (): MentionsHistoryRestore | null => {\n    const history = ref.current;\n    if (history.index <= 0) {\n      return null;\n    }\n\n    const undone = history.entries[history.index];\n    history.index -= 1;\n    history.lastType = null;\n    return { value: history.entries[history.index].value, caret: undone.caretBefore };\n  };\n\n  const redo = (): MentionsHistoryRestore | null => {\n    const history = ref.current;\n    if (history.index >= history.entries.length - 1) {\n      return null;\n    }\n\n    history.index += 1;\n    history.lastType = null;\n    const entry = history.entries[history.index];\n    return { value: entry.value, caret: entry.caret };\n  };\n\n  const syncExternal = (value: string, caret: number) => {\n    push(value, caret, caret, 'external');\n  };\n\n  return { push, undo, redo, syncExternal };\n}\n"],"mappings":";;;AAyBA,MAAM,cAAc;AACpB,MAAM,cAAc;AAEpB,SAAgB,mBAAmB,cAAsB,cAAsB;CAC7E,MAAM,MAAM,OAA6B,IAAuC;CAEhF,IAAI,IAAI,YAAY,MAClB,IAAI,UAAU;EACZ,SAAS,CAAC;GAAE,OAAO;GAAc,OAAO;GAAc,aAAa;EAAa,CAAC;EACjF,OAAO;EACP,UAAU;EACV,UAAU;CACZ;CAGF,MAAM,QACJ,OACA,OACA,aACA,SACG;EACH,MAAM,UAAU,IAAI;EACpB,MAAM,MAAM,KAAK,IAAI;EACrB,MAAM,UAAU,QAAQ,QAAQ,QAAQ;EACxC,IAAI,QAAQ,UAAU,OACpB;EAMF,IAFE,SAAS,WAAW,QAAQ,aAAa,WAAW,MAAM,QAAQ,WAAW,aAG7E,QAAQ,QAAQ,QAAQ,SAAS;GAAE;GAAO;GAAO,aAAa,QAAQ;EAAY;OAC7E;GACL,QAAQ,UAAU,QAAQ,QAAQ,MAAM,GAAG,QAAQ,QAAQ,CAAC;GAC5D,QAAQ,QAAQ,KAAK;IAAE;IAAO;IAAO;GAAY,CAAC;GAClD,IAAI,QAAQ,QAAQ,SAAS,aAC3B,QAAQ,QAAQ,MAAM;GAExB,QAAQ,QAAQ,QAAQ,QAAQ,SAAS;EAC3C;EAEA,QAAQ,WAAW;EACnB,QAAQ,WAAW;CACrB;CAEA,MAAM,aAA4C;EAChD,MAAM,UAAU,IAAI;EACpB,IAAI,QAAQ,SAAS,GACnB,OAAO;EAGT,MAAM,SAAS,QAAQ,QAAQ,QAAQ;EACvC,QAAQ,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