import { jsx as A } from "react/jsx-runtime";
import { AIBudgetEnd as k } from "./index-AIBudgetEnd.js";
import { aiBudgetExceededMessage as y } from "./index-AIBudgetEnd.data.js";
import { getPathId as C } from "./index-get-path-id.js";
import { AITextObjectType as b, AITextOutputFormat as x } from "./index-AIText.js";
import { KeywordsObjectType as G } from "./index-Keywords.js";
import { supportService as O } from "./index-support.service.js";
import { aiStore as P } from "./index-AiStore.js";
import { IconNames as K } from "./index-IconNames.js";
import { ModalService as w } from "./index-ModalService.js";
import { ErrorModal as M } from "./index-ErrorModal.js";
import { useState as c, useEffect as j } from "react";
import { mapAdjustOptionsToBE as B } from "./index-ai-assistent-adjust-options.js";
const Y = (S) => {
  const {
    generateCallback: d,
    adjustCallback: p,
    setValue: E,
    onPanelStatusChange: l,
    useHtml: I,
    keywordsToInclude: m,
    keepShort: g
  } = S, [r, o] = c("closed");
  j(() => {
    l && l(r);
  }, [r]);
  const [u, n] = c(""), [e, s] = c(0), [_, f] = c(!1);
  return j(() => {
    e && r === "writing" && (u && u !== "" && E(u), o("open"));
  }, [e]), {
    panelStatus: r,
    setPanelStatus: o,
    generateCallback: d,
    generatedCount: e,
    generateError: _,
    onGenerateClick: (i) => {
      o("writing"), r !== "writing" && d(i, g).then((t) => {
        n(t), s(e + 1), f(!1);
      }, (t) => {
        o("closed"), n(""), s(e + 1), f(!0);
      });
    },
    onAdjustClick: (i, t, T) => {
      o("writing"), r !== "writing" && (p ? p(i, t, T).then((a) => {
        n(a), s(e + 1);
      }, (a) => {
        n(""), s(e + 1);
      }) : P.postClientAiTextAdjustByLocationId(C(), {
        aiText: {
          outputFormat: I ? x.HTML_TAGS : x.MARKDOWN,
          text: i,
          objectType: b.App_Domain_AI_Entities_Text_AIText
        },
        command: T,
        expand: t === "expand",
        shorten: t === "shorten",
        generateHashtags: t === "generateHashtags",
        keywordsToIntegrate: t === "includeKeywords" && m ? {
          elements: m,
          objectType: G.App_Domain_Common_Entities_Keywords_Keywords
        } : void 0,
        tone: B(t),
        keepShort: g
      }, {}).subscribe({
        next: (a) => {
          var h;
          n(((h = a.aiText) == null ? void 0 : h.adjustedText) ?? ""), s(e + 1);
        },
        error: (a) => {
          if (o("closed"), a.response.data.error === y) {
            w.open(A(k, {
              close: () => {
              }
            }));
            return;
          }
          w.open(A(M, {
            close: () => null,
            source: "AI_ASSISTANT",
            title: "Oh snap! Something went wrong!",
            message: "The application has encountered an unknown error. Please contact customer support for further help.",
            ctaPositive: {
              text: "contact our support",
              cta: () => {
                O.show();
              },
              icon: K.arrowRight
            },
            ctaPosition: "center"
          })), n(""), s(e + 1);
        }
      }));
    }
  };
};
export {
  Y as useAiAssistantAdjustAndGenerate
};
