const buildPrompt = ({
  userPrompt,
  widgetType,
  isWidget,
  colorPalette,
  documentContext
}) => {
  const [backgroundColor, ...paletteColors] = colorPalette ?? [];
  const colorPaletteText = backgroundColor ? `

Use ${backgroundColor} for background. Brand's color palette: ${paletteColors.join(", ")}.` : "";
  const placementSuffix = isWidget ? ", widget is placed in <WIDGET_PLACEMENT> tag. Try to use the information that is closest to the <WIDGET_PLACEMENT> tag from the <context>" : "";
  const widthRule = isWidget ? "\n\nThe widget must fill the full width of the document. Use width: 100% on the outermost container." : "";
  const contextBlock = isWidget && documentContext?.trim() ? `

<context>
${documentContext}
<WIDGET_PLACEMENT>
</context>` : "";
  if (widgetType) {
    return [
      userPrompt,
      placementSuffix,
      `

 **IMPORTANT**
The type of widget to create should be of type ${widgetType}, RELEVANT to the content, its styling and request mentioned above`,
      colorPaletteText,
      widthRule,
      contextBlock
    ].join("");
  }
  return [userPrompt, placementSuffix, colorPaletteText, widthRule, contextBlock].join("");
};
const getBaseUrl = () => typeof location !== "undefined" && location.hostname === "editor.wix.com" ? "https://editor.wix.com/_api/ricos-ai-assistant" : "https://www.wixapis.com/ricos";
const parseResponse = (body, isWidget) => {
  if (isWidget) {
    const { widgetId } = body;
    if (!widgetId) {
      return { type: "ERROR", error: "No widgetId returned" };
    }
    return {
      type: "SUCCESS",
      widgetId,
      containerData: {
        alignment: "CENTER",
        textWrap: true,
        width: { custom: "900" },
        height: { custom: "474" }
      }
    };
  }
  const { html } = body;
  if (!html) {
    return { type: "ERROR", error: "Error generating HTML" };
  }
  return {
    type: "SUCCESS",
    html,
    containerData: {
      alignment: "CENTER",
      textWrap: true,
      width: { custom: body.width?.toString() ?? "900" },
      height: { custom: body.height?.toString() ?? "474" }
    }
  };
};
const doPrompt = async ({
  prompt: userPrompt,
  files,
  document,
  selection,
  makeRequest,
  widgetType,
  outputMode,
  colorPalette,
  documentContext,
  onRequestFailed
}) => {
  const isWidget = outputMode === "WIDGET";
  const prompt = buildPrompt({ userPrompt, widgetType, isWidget, colorPalette, documentContext });
  try {
    const requestBody = {
      ricosConversation: {
        document,
        documentSelection: { nodes: selection },
        returnMetadata: true,
        files,
        message: prompt
      },
      ...outputMode ? { outputMode } : {}
    };
    const response = await makeRequest(`${getBaseUrl()}/v1/ricos-conversations/html`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Accept: "application/x-ndjson"
      },
      body: JSON.stringify(requestBody)
    });
    const requestId = response.headers.get("x-wix-request-id");
    if (requestId) {
      console.log(
        "\x1B[44m\x1B[97m RICOS PROMPT \x1B[0m",
        prompt,
        `https://grafana.wixpress.com/d/dewd4kmq6f400b/ricos-ai-assistant?var-selected_request_id=${requestId}`
      );
    }
    if (!response.ok) {
      onRequestFailed?.({ status: response.status, response });
      return { type: "ERROR", error: "Error generating response" };
    }
    const body = await response.json();
    return parseResponse(body, isWidget);
  } catch (error) {
    return {
      type: "ERROR",
      error: error instanceof Error ? error.message : "Error generating response"
    };
  }
};
export {
  doPrompt
};
//# sourceMappingURL=do-prompt.js.map
