"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
const designSystem = require("@choiceform/design-system");
const index = require("../../../../i18n/index.cjs");
const index$1 = require("../../../../_vendor/usehooks-ts/dist-index.cjs");
const promptInputContext = require("../../context/prompt-input-context.cjs");
const attachment = require("../../../../types/attachment.cjs");
const PasteFileUrlDialog = React.memo(function PasteFileUrlDialog2(props) {
  const { isOpen, onClose } = props;
  const [url, setUrl] = React.useState("");
  const [showError, setShowError] = React.useState(false);
  const [isUploading, setIsUploading] = React.useState(false);
  const { onFileUploadByUrl, onFileUploadError, dragCtx } = promptInputContext.usePromptInputContext();
  const pasteFileUrl = index.t("prompt-input", "paste-file-url");
  const pasteFileUrlPlaceholder = index.t("prompt-input", "paste-file-url-placeholder");
  const invalidFileUrl = index.t("prompt-input", "invalid-file-url");
  const isValidUrl = index$1.useEventCallback((urlString) => {
    try {
      new URL(urlString);
      return true;
    } catch {
      return false;
    }
  });
  const uploadFileByUrl = index$1.useEventCallback(async (url2) => {
    if (!onFileUploadByUrl) {
      console.error("Upload function not provided");
      return;
    }
    const result = await onFileUploadByUrl(url2);
    if (!result) {
      throw new Error("File upload failed");
    }
    if (dragCtx?.addAttachments) {
      const attachment$1 = {
        ...result,
        type: result.mimeType?.startsWith("image/") ? attachment.AttachmentType.Image : attachment.AttachmentType.File
      };
      dragCtx.addAttachments([attachment$1]);
    }
  });
  const handleConfirm = index$1.useEventCallback(async () => {
    if (!url.trim()) return;
    if (!isValidUrl(url)) {
      setShowError(true);
      return;
    }
    if (!onFileUploadByUrl) {
      console.error("Upload function not provided");
      return;
    }
    setIsUploading(true);
    try {
      await uploadFileByUrl(url.trim());
      onClose();
      setUrl("");
      setShowError(false);
    } catch (error) {
      const errorMessage = error instanceof Error ? error.message : "Upload failed";
      setShowError(true);
      if (onFileUploadError) {
        onFileUploadError(errorMessage);
      }
    } finally {
      setIsUploading(false);
    }
  });
  const handleClose = index$1.useEventCallback(() => {
    setUrl("");
    setShowError(false);
    onClose();
  });
  const handleDialogOpenChange = index$1.useEventCallback((open) => {
    if (!open) {
      handleClose();
    }
  });
  return /* @__PURE__ */ jsxRuntime.jsxs(
    designSystem.Dialog,
    {
      open: isOpen,
      onOpenChange: handleDialogOpenChange,
      children: [
        /* @__PURE__ */ jsxRuntime.jsx(
          designSystem.Dialog.Header,
          {
            title: pasteFileUrl,
            onClose: () => {
              handleClose();
            }
          }
        ),
        /* @__PURE__ */ jsxRuntime.jsx(
          designSystem.Dialog.Content,
          {
            className: "w-md space-y-4 p-4",
            onClick: (e) => {
              e.stopPropagation();
            },
            children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-1.5", children: [
              /* @__PURE__ */ jsxRuntime.jsx(
                designSystem.TextField,
                {
                  placeholder: pasteFileUrlPlaceholder,
                  value: url,
                  onChange: (value) => {
                    setShowError(false);
                    setUrl(value.trim());
                  },
                  onKeyDown: (e) => {
                    if (e.key === "Enter" && url.trim()) {
                      handleConfirm();
                    }
                  },
                  autoFocus: true
                }
              ),
              showError && /* @__PURE__ */ jsxRuntime.jsx("span", { className: "text-danger-foreground text-xs", children: invalidFileUrl })
            ] })
          }
        ),
        /* @__PURE__ */ jsxRuntime.jsxs(designSystem.Dialog.Footer, { className: "flex justify-end gap-2", children: [
          /* @__PURE__ */ jsxRuntime.jsx(
            designSystem.Button,
            {
              variant: "secondary",
              onClick: handleClose,
              children: index.t("common", "cancel")
            }
          ),
          /* @__PURE__ */ jsxRuntime.jsx(
            designSystem.Button,
            {
              onClick: handleConfirm,
              disabled: !url.trim() || isUploading,
              loading: isUploading,
              children: index.t("common", "confirm")
            }
          )
        ] })
      ]
    }
  );
});
exports.PasteFileUrlDialog = PasteFileUrlDialog;
//# sourceMappingURL=paste-file-url-dialog.cjs.map
