import { jsxs, jsx } from "react/jsx-runtime";
import { memo, useState } from "react";
import { Dialog, TextField, Button } from "@choiceform/design-system";
import { t } from "../../../../i18n/index.js";
import { useEventCallback } from "../../../../_vendor/usehooks-ts/dist-index.js";
import { usePromptInputContext } from "../../context/prompt-input-context.js";
import { AttachmentType } from "../../../../types/attachment.js";
const PasteFileUrlDialog = memo(function PasteFileUrlDialog2(props) {
  const { isOpen, onClose } = props;
  const [url, setUrl] = useState("");
  const [showError, setShowError] = useState(false);
  const [isUploading, setIsUploading] = useState(false);
  const { onFileUploadByUrl, onFileUploadError, dragCtx } = usePromptInputContext();
  const pasteFileUrl = t("prompt-input", "paste-file-url");
  const pasteFileUrlPlaceholder = t("prompt-input", "paste-file-url-placeholder");
  const invalidFileUrl = t("prompt-input", "invalid-file-url");
  const isValidUrl = useEventCallback((urlString) => {
    try {
      new URL(urlString);
      return true;
    } catch {
      return false;
    }
  });
  const uploadFileByUrl = 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 = {
        ...result,
        type: result.mimeType?.startsWith("image/") ? AttachmentType.Image : AttachmentType.File
      };
      dragCtx.addAttachments([attachment]);
    }
  });
  const handleConfirm = 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 = useEventCallback(() => {
    setUrl("");
    setShowError(false);
    onClose();
  });
  const handleDialogOpenChange = useEventCallback((open) => {
    if (!open) {
      handleClose();
    }
  });
  return /* @__PURE__ */ jsxs(
    Dialog,
    {
      open: isOpen,
      onOpenChange: handleDialogOpenChange,
      children: [
        /* @__PURE__ */ jsx(
          Dialog.Header,
          {
            title: pasteFileUrl,
            onClose: () => {
              handleClose();
            }
          }
        ),
        /* @__PURE__ */ jsx(
          Dialog.Content,
          {
            className: "w-md space-y-4 p-4",
            onClick: (e) => {
              e.stopPropagation();
            },
            children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-1.5", children: [
              /* @__PURE__ */ jsx(
                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__ */ jsx("span", { className: "text-danger-foreground text-xs", children: invalidFileUrl })
            ] })
          }
        ),
        /* @__PURE__ */ jsxs(Dialog.Footer, { className: "flex justify-end gap-2", children: [
          /* @__PURE__ */ jsx(
            Button,
            {
              variant: "secondary",
              onClick: handleClose,
              children: t("common", "cancel")
            }
          ),
          /* @__PURE__ */ jsx(
            Button,
            {
              onClick: handleConfirm,
              disabled: !url.trim() || isUploading,
              loading: isUploading,
              children: t("common", "confirm")
            }
          )
        ] })
      ]
    }
  );
});
export {
  PasteFileUrlDialog
};
//# sourceMappingURL=paste-file-url-dialog.js.map
