{"version":3,"file":"use-mentions-value.mjs","names":[],"sources":["../../../src/components/Mentions/use-mentions-value.ts"],"sourcesContent":["import { useEffect, useRef } from 'react';\nimport { useUncontrolled } from '@mantine/hooks';\nimport type { MentionsChangePayload, MentionsChangeReason } from './Mentions.types';\nimport { useMentionsHistory, type MentionsHistoryChangeType } from './use-mentions-history';\nimport { toMentionOccurrence } from './utils/find-mention-occurrences';\nimport { getIdText } from './utils/get-plain-text';\nimport { normalizeNewlines } from './utils/normalize-newlines';\nimport type { MentionConfig } from './utils/types';\nimport { walkMentions, type MentionWalk } from './utils/walk-mentions';\n\ninterface UseMentionsValueInput {\n  value: string | undefined;\n  defaultValue: string | undefined;\n  onChange: ((payload: MentionsChangePayload) => void) | undefined;\n  singleLine: boolean;\n  configs: MentionConfig[];\n  setCaret: (caret: number, walk: MentionWalk) => void;\n}\n\ninterface PendingEdit {\n  value: string;\n  caret: number;\n  caretBefore: number;\n  type: MentionsHistoryChangeType;\n}\n\nexport function useMentionsValue({\n  value,\n  defaultValue,\n  onChange,\n  singleLine,\n  configs,\n  setCaret,\n}: UseMentionsValueInput) {\n  const [_value, setValueState] = useUncontrolled({\n    value: value === undefined ? undefined : normalizeNewlines(value, singleLine),\n    defaultValue:\n      defaultValue === undefined ? undefined : normalizeNewlines(defaultValue, singleLine),\n    finalValue: '',\n  });\n\n  const walk = walkMentions(_value, configs);\n  const history = useMentionsHistory(_value, walk.plainText.length);\n  const pendingEditRef = useRef<PendingEdit | null>(null);\n\n  const applyEdit = (\n    newValue: string,\n    caret: number,\n    caretBefore: number,\n    reason: MentionsChangeReason,\n    mentionId?: string\n  ) => {\n    const nextWalk = walkMentions(normalizeNewlines(newValue, singleLine), configs);\n    setValueState(nextWalk.value);\n    onChange?.({\n      value: nextWalk.value,\n      plainTextValue: nextWalk.plainText,\n      idValue: getIdText(nextWalk, configs),\n      mentions: nextWalk.spans.map(toMentionOccurrence),\n      mentionId,\n      previousValue: _value,\n      reason,\n    });\n    pendingEditRef.current =\n      reason.type === 'undo' || reason.type === 'redo'\n        ? null\n        : { value: nextWalk.value, caret, caretBefore, type: reason.type };\n    setCaret(caret, nextWalk);\n    return nextWalk;\n  };\n\n  useEffect(() => {\n    const pending = pendingEditRef.current;\n    pendingEditRef.current = null;\n    if (pending && pending.value === _value) {\n      history.push(_value, pending.caret, pending.caretBefore, pending.type);\n    } else {\n      history.syncExternal(_value, walk.plainText.length);\n    }\n  }, [_value]);\n\n  return {\n    walk,\n    applyEdit,\n    undo: history.undo,\n    redo: history.redo,\n  };\n}\n"],"mappings":";;;;;;;;;AA0BA,SAAgB,iBAAiB,EAC/B,OACA,cACA,UACA,YACA,SACA,YACwB;CACxB,MAAM,CAAC,QAAQ,iBAAiB,gBAAgB;EAC9C,OAAO,UAAU,KAAA,IAAY,KAAA,IAAY,kBAAkB,OAAO,UAAU;EAC5E,cACE,iBAAiB,KAAA,IAAY,KAAA,IAAY,kBAAkB,cAAc,UAAU;EACrF,YAAY;CACd,CAAC;CAED,MAAM,OAAO,aAAa,QAAQ,OAAO;CACzC,MAAM,UAAU,mBAAmB,QAAQ,KAAK,UAAU,MAAM;CAChE,MAAM,iBAAiB,OAA2B,IAAI;CAEtD,MAAM,aACJ,UACA,OACA,aACA,QACA,cACG;EACH,MAAM,WAAW,aAAa,kBAAkB,UAAU,UAAU,GAAG,OAAO;EAC9E,cAAc,SAAS,KAAK;EAC5B,WAAW;GACT,OAAO,SAAS;GAChB,gBAAgB,SAAS;GACzB,SAAS,UAAU,UAAU,OAAO;GACpC,UAAU,SAAS,MAAM,IAAI,mBAAmB;GAChD;GACA,eAAe;GACf;EACF,CAAC;EACD,eAAe,UACb,OAAO,SAAS,UAAU,OAAO,SAAS,SACtC,OACA;GAAE,OAAO,SAAS;GAAO;GAAO;GAAa,MAAM,OAAO;EAAK;EACrE,SAAS,OAAO,QAAQ;EACxB,OAAO;CACT;CAEA,gBAAgB;EACd,MAAM,UAAU,eAAe;EAC/B,eAAe,UAAU;EACzB,IAAI,WAAW,QAAQ,UAAU,QAC/B,QAAQ,KAAK,QAAQ,QAAQ,OAAO,QAAQ,aAAa,QAAQ,IAAI;OAErE,QAAQ,aAAa,QAAQ,KAAK,UAAU,MAAM;CAEtD,GAAG,CAAC,MAAM,CAAC;CAEX,OAAO;EACL;EACA;EACA,MAAM,QAAQ;EACd,MAAM,QAAQ;CAChB;AACF"}