"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var ApprovalPrompt_exports = {};
__export(ApprovalPrompt_exports, {
  ApprovalPrompt: () => ApprovalPrompt
});
module.exports = __toCommonJS(ApprovalPrompt_exports);
var import_jsx_runtime = require("react/jsx-runtime");
var import_react = require("react");
var import_ink = require("ink");
var import_core = require("@dexto/core");
var import_ElicitationForm = require("./ElicitationForm.js");
var import_renderers = require("./renderers/index.js");
var import_toolUtils = require("../utils/toolUtils.js");
var import_messageFormatting = require("../utils/messageFormatting.js");
const ApprovalPrompt = (0, import_react.forwardRef)(
  ({ approval, onApprove, onDeny, onCancel }, ref) => {
    const isCommandApproval = approval.type === import_core.ApprovalType.COMMAND_APPROVAL;
    const isElicitation = approval.type === "elicitation";
    const toolName = approval.metadata.toolName;
    const toolArgs = approval.metadata.args || {};
    const approvalKey = typeof approval.metadata.approvalKey === "string" ? approval.metadata.approvalKey : void 0;
    const hasApprovalKey = approvalKey !== void 0;
    const callDescriptionRaw = typeof approval.metadata.description === "string" ? approval.metadata.description : void 0;
    const callDescription = (0, import_react.useMemo)(() => {
      if (!callDescriptionRaw) return null;
      const singleLine = callDescriptionRaw.replace(/\s+/g, " ").trim();
      if (!singleLine) return null;
      const maxLength = 120;
      if (singleLine.length <= maxLength) return singleLine;
      return `${singleLine.slice(0, Math.max(0, maxLength - 1))}\u2026`;
    }, [callDescriptionRaw]);
    const isPlanReview = toolName === "plan_review";
    const isEditOrWriteTool = (0, import_toolUtils.isEditWriteTool)(toolName);
    const presentationSnapshot = (0, import_react.useMemo)(() => {
      const raw = approval.metadata.presentationSnapshot;
      if (typeof raw !== "object" || raw === null) {
        return void 0;
      }
      if (raw.version !== 1) {
        return void 0;
      }
      return raw;
    }, [approval.metadata]);
    const formattedTool = (0, import_react.useMemo)(() => {
      if (!toolName) return null;
      return (0, import_messageFormatting.formatToolHeader)({
        toolName,
        args: toolArgs,
        ...presentationSnapshot !== void 0 && { presentationSnapshot }
      });
    }, [toolName, toolArgs, presentationSnapshot]);
    const [selectedIndex, setSelectedIndex] = (0, import_react.useState)(0);
    const [showFeedbackInput, setShowFeedbackInput] = (0, import_react.useState)(false);
    const [feedbackText, setFeedbackText] = (0, import_react.useState)("");
    const elicitationFormRef = (0, import_react.useRef)(null);
    const selectedIndexRef = (0, import_react.useRef)(0);
    const options = [];
    if (isPlanReview) {
      options.push({ id: "plan-approve", label: "Approve" });
      options.push({ id: "plan-approve-accept-edits", label: "Approve + Accept All Edits" });
    } else if (isCommandApproval) {
      options.push({ id: "yes", label: "Yes" });
      options.push({ id: "no", label: "No" });
    } else if (hasApprovalKey) {
      options.push({ id: "yes", label: "Yes (once)" });
      options.push({ id: "yes-session", label: "Yes, remember this approval" });
      options.push({ id: "no", label: "No" });
    } else if (isEditOrWriteTool) {
      options.push({ id: "yes", label: "Yes" });
      options.push({ id: "yes-accept-edits", label: "Yes, and accept all edits" });
      options.push({ id: "no", label: "No" });
    } else {
      options.push({ id: "yes", label: "Yes" });
      options.push({ id: "yes-session", label: "Yes (Session)" });
      options.push({ id: "no", label: "No" });
    }
    (0, import_react.useEffect)(() => {
      selectedIndexRef.current = selectedIndex;
    }, [selectedIndex]);
    const getCurrentOption = () => options[selectedIndexRef.current];
    (0, import_react.useImperativeHandle)(
      ref,
      () => ({
        handleInput: (input, key) => {
          if (isElicitation && elicitationFormRef.current) {
            return elicitationFormRef.current.handleInput(input, key);
          }
          const totalOptions = isPlanReview ? options.length + 1 : options.length;
          const isFeedbackSelected = isPlanReview && selectedIndexRef.current === options.length;
          if (isFeedbackSelected) {
            if (key.return) {
              onDeny(feedbackText || void 0);
              return true;
            } else if (key.backspace || key.delete) {
              setFeedbackText((prev) => prev.slice(0, -1));
              return true;
            } else if (key.upArrow) {
              setSelectedIndex(options.length - 1);
              return true;
            } else if (key.downArrow) {
              setSelectedIndex(0);
              return true;
            } else if (key.escape) {
              onCancel();
              return true;
            } else if (input && !key.ctrl && !key.meta) {
              setFeedbackText((prev) => prev + input);
              return true;
            }
            return true;
          }
          if (key.upArrow) {
            setSelectedIndex(
              (current) => current === 0 ? totalOptions - 1 : current - 1
            );
            return true;
          } else if (key.downArrow) {
            setSelectedIndex(
              (current) => current === totalOptions - 1 ? 0 : current + 1
            );
            return true;
          } else if (key.return) {
            const option = getCurrentOption();
            if (!option) return false;
            if (option.id === "plan-approve") {
              onApprove({});
            } else if (option.id === "plan-approve-accept-edits") {
              onApprove({ enableAcceptEditsMode: true });
            } else if (option.id === "yes") {
              onApprove({});
            } else if (option.id === "yes-session") {
              onApprove({ rememberChoice: true });
            } else if (option.id === "yes-accept-edits") {
              onApprove({ enableAcceptEditsMode: true });
            } else if (option.id === "no") {
              onDeny();
            }
            return true;
          } else if (key.shift && key.tab && isEditOrWriteTool) {
            onApprove({ enableAcceptEditsMode: true });
            return true;
          } else if (key.escape) {
            onCancel();
            return true;
          }
          return false;
        }
      }),
      [
        isElicitation,
        isEditOrWriteTool,
        isPlanReview,
        options,
        onApprove,
        onDeny,
        onCancel,
        feedbackText
      ]
    );
    if (isElicitation) {
      const metadata = approval.metadata;
      return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
        import_ElicitationForm.ElicitationForm,
        {
          ref: elicitationFormRef,
          metadata,
          onSubmit: (formData) => onApprove({ formData }),
          onCancel
        }
      );
    }
    const command = approval.metadata.command;
    const displayPreview = approval.metadata.displayPreview;
    const renderPreview = () => {
      if (!displayPreview) return null;
      switch (displayPreview.type) {
        case "diff":
          return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_renderers.DiffPreview, { data: displayPreview });
        case "shell":
          return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { flexDirection: "column", marginBottom: 1, children: [
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { marginBottom: 0, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "cyan", bold: true, children: displayPreview.title ?? "Bash" }) }),
            /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(
              import_ink.Box,
              {
                flexDirection: "column",
                borderStyle: "round",
                borderColor: "gray",
                paddingX: 1,
                children: [
                  displayPreview.command.split("\n").map((line, i) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "yellowBright", wrap: "wrap", children: [
                    i === 0 ? "$ " : "  ",
                    line
                  ] }, i)),
                  displayPreview.isBackground && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "gray", children: "(background)" })
                ]
              }
            ),
            callDescription && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { marginTop: 0, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "gray", children: callDescription }) })
          ] });
        case "file":
          if (displayPreview.content) {
            return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_renderers.CreateFilePreview, { data: displayPreview });
          }
          return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { marginBottom: 1, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "gray", children: [
            displayPreview.operation === "read" && `Read ${displayPreview.lineCount ?? "file"} lines`,
            displayPreview.operation === "write" && `Write to ${displayPreview.path}`,
            displayPreview.operation === "delete" && `Delete ${displayPreview.path}`
          ] }) });
        default:
          return null;
      }
    };
    const showHeaderBlock = isCommandApproval || !displayPreview;
    return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { paddingX: 0, paddingY: 0, flexDirection: "column", children: [
      showHeaderBlock && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { flexDirection: "column", marginBottom: 0, children: isCommandApproval ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
        /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { flexDirection: "row", children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "yellowBright", bold: true, children: [
          "Confirm command:",
          " "
        ] }) }),
        command && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { flexDirection: "column", marginTop: 0, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
          import_ink.Box,
          {
            flexDirection: "column",
            borderStyle: "round",
            borderColor: "gray",
            paddingX: 1,
            children: command.split("\n").map((line, i) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "red", wrap: "wrap", children: line }, i))
          }
        ) })
      ] }) : /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
        /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { flexDirection: "row", children: [
          /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "yellowBright", bold: true, children: [
            "Confirm:",
            " "
          ] }),
          formattedTool && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "cyan", children: formattedTool.header })
        ] }),
        callDescription && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { flexDirection: "row", marginTop: 0, children: [
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "gray", children: "  " }),
          /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "gray", children: callDescription })
        ] })
      ] }) }),
      renderPreview(),
      /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { flexDirection: "column", marginTop: 0, children: [
        options.map((option, index) => {
          const isSelected = index === selectedIndex;
          const isNo = option.id === "no";
          return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { children: isSelected ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: isNo ? "red" : "green", bold: true, children: [
            "  \u25B6 ",
            option.label
          ] }) : /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "gray", children: [
            "    ",
            option.label
          ] }) }, option.id);
        }),
        isPlanReview && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { children: selectedIndex === options.length ? (
          // Selected - show editable input
          /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Box, { flexDirection: "row", children: [
            /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "red", bold: true, children: "  \u25B6 " }),
            feedbackText ? /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "white", children: [
              feedbackText,
              /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "cyan", children: "\u258B" })
            ] }) : /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "gray", children: [
              "What changes would you like?",
              /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Text, { color: "cyan", children: "\u258B" })
            ] })
          ] })
        ) : (
          // Not selected - show placeholder
          /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "gray", children: [
            "    ",
            feedbackText || "What changes would you like?"
          ] })
        ) })
      ] }),
      /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_ink.Box, { marginTop: 0, children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ink.Text, { color: "gray", children: [
        "  ",
        "\u2191\u2193 to select \u2022 Enter to confirm \u2022 Esc to cancel"
      ] }) })
    ] });
  }
);
ApprovalPrompt.displayName = "ApprovalPrompt";
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ApprovalPrompt
});
