{"version":3,"sources":["../src/hooks/base-ai-textarea-implementation/use-populate-ai-textarea-ref.ts"],"sourcesContent":["import React from \"react\";\nimport { Editor } from \"slate\";\nimport { ReactEditor } from \"slate-react\";\nimport { getFullEditorTextWithNewlines } from \"../../lib/get-text-around-cursor\";\nimport { replaceEditorText } from \"../../lib/slatejs-edits/replace-text\";\nimport { HTMLAiTextAreaElement } from \"../../types\";\nimport { CustomEditor } from \"../../types/base/custom-editor\";\n\nexport function usePopulateAiTextareaRef(\n  editor: Editor,\n  ref: React.Ref<HTMLAiTextAreaElement>,\n) {\n  React.useImperativeHandle(ref, () => {\n    class Combined {\n      constructor(\n        private customMethods: CustomMethods,\n        private editorHtmlElement: HTMLElement,\n      ) {}\n\n      [key: string]: any;\n\n      get(target: any, propKey: string): any {\n        if (this.isKeyOfCustomMethods(propKey)) {\n          const value = this.customMethods[propKey];\n          if (typeof value === \"function\") {\n            return value.bind(this.customMethods);\n          }\n          return value;\n        } else if (this.isKeyOfHTMLElement(propKey)) {\n          const value = this.editorHtmlElement[propKey];\n          if (typeof value === \"function\") {\n            return value.bind(this.editorHtmlElement);\n          }\n          return value;\n        }\n      }\n\n      set(target: any, propKey: string, value: any): boolean {\n        if (this.isKeyOfCustomMethods(propKey)) {\n          (this.customMethods as any)[propKey] = value;\n        } else if (this.isKeyOfHTMLElement(propKey)) {\n          (this.editorHtmlElement as any)[propKey] = value;\n        } else {\n          // Default behavior (optional)\n          target[propKey] = value;\n        }\n        return true;\n      }\n\n      private isKeyOfCustomMethods(key: string): key is keyof CustomMethods {\n        return key in this.customMethods;\n      }\n\n      private isKeyOfHTMLElement(key: string): key is keyof HTMLElement {\n        return key in this.editorHtmlElement;\n      }\n    }\n\n    const handler = {\n      get(target: any, propKey: keyof CustomMethods | keyof HTMLElement) {\n        return target.get(target, propKey);\n      },\n      set(target: any, propKey: keyof CustomMethods | keyof HTMLElement, value: any) {\n        return target.set(target, propKey, value);\n      },\n    };\n\n    class CustomMethods {\n      constructor(private editor: CustomEditor) {}\n\n      focus() {\n        ReactEditor.focus(this.editor);\n      }\n\n      blur() {\n        ReactEditor.blur(this.editor);\n      }\n\n      get value() {\n        return getFullEditorTextWithNewlines(this.editor);\n      }\n      set value(value: string) {\n        replaceEditorText(this.editor, value);\n      }\n    }\n\n    const editorHtmlElement = ReactEditor.toDOMNode(editor, editor);\n    const customMethods = new CustomMethods(editor);\n\n    const combined = new Combined(customMethods, editorHtmlElement);\n    return new Proxy(combined, handler);\n  }, [editor]);\n}\n"],"mappings":";;;;;;;;AAAA,OAAO,WAAW;AAElB,SAAS,mBAAmB;AAMrB,SAAS,yBACd,QACA,KACA;AACA,QAAM,oBAAoB,KAAK,MAAM;AACnC,UAAM,SAAS;AAAA,MACb,YACUA,gBACAC,oBACR;AAFQ,6BAAAD;AACA,iCAAAC;AAAA,MACP;AAAA,MAIH,IAAI,QAAa,SAAsB;AACrC,YAAI,KAAK,qBAAqB,OAAO,GAAG;AACtC,gBAAM,QAAQ,KAAK,cAAc,OAAO;AACxC,cAAI,OAAO,UAAU,YAAY;AAC/B,mBAAO,MAAM,KAAK,KAAK,aAAa;AAAA,UACtC;AACA,iBAAO;AAAA,QACT,WAAW,KAAK,mBAAmB,OAAO,GAAG;AAC3C,gBAAM,QAAQ,KAAK,kBAAkB,OAAO;AAC5C,cAAI,OAAO,UAAU,YAAY;AAC/B,mBAAO,MAAM,KAAK,KAAK,iBAAiB;AAAA,UAC1C;AACA,iBAAO;AAAA,QACT;AAAA,MACF;AAAA,MAEA,IAAI,QAAa,SAAiB,OAAqB;AACrD,YAAI,KAAK,qBAAqB,OAAO,GAAG;AACtC,UAAC,KAAK,cAAsB,OAAO,IAAI;AAAA,QACzC,WAAW,KAAK,mBAAmB,OAAO,GAAG;AAC3C,UAAC,KAAK,kBAA0B,OAAO,IAAI;AAAA,QAC7C,OAAO;AAEL,iBAAO,OAAO,IAAI;AAAA,QACpB;AACA,eAAO;AAAA,MACT;AAAA,MAEQ,qBAAqB,KAAyC;AACpE,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