import { Box, CloseButton, Image, Loader, StatusIndicator, Text } from "@wix/design-system";
import { DataCollection, Document } from "@wix/wix-ui-icons-common";
import classNames from "classnames";
import React, { useContext } from "react";
import { RicosContext } from "./../../../ricos-context/index.js";
import { unreachable } from "./../../uncreachable.js";
import classes from "./attachment-bar-scss.js";
const resolveImageUrl = (url) => {
  if (url.startsWith("http://") || url.startsWith("https://")) {
    return url;
  }
  const mediaId = url.startsWith("media/") ? url : `media/${url}`;
  return `https://static.wixstatic.com/${mediaId}`;
};
const AttachmentBar = ({ attachments, onRemove }) => {
  return /* @__PURE__ */ React.createElement("div", { className: classes.root }, attachments.map(
    (attachment) => ["png", "jpg", "jpeg"].includes(attachment.file.type) ? /* @__PURE__ */ React.createElement(ImageAttachment, { key: attachment.id, attachment, onRemove }) : /* @__PURE__ */ React.createElement(FileAttachment, { key: attachment.id, attachment, onRemove })
  ));
};
const ImageAttachment = ({
  attachment,
  onRemove
}) => {
  const { t } = useContext(RicosContext);
  return /* @__PURE__ */ React.createElement("div", { className: classes.attachmentWrapper }, (() => {
    switch (attachment.status) {
      case "ok": {
        return /* @__PURE__ */ React.createElement(
          Image,
          {
            src: resolveImageUrl(attachment.file.url),
            showBorder: true,
            width: 48,
            height: "100%"
          }
        );
      }
      case "loading": {
        return /* @__PURE__ */ React.createElement("div", { className: classes.statusIconContainer }, /* @__PURE__ */ React.createElement(Loader, { size: "tiny" }));
      }
      case "error": {
        return /* @__PURE__ */ React.createElement("div", { className: classes.statusIconContainer }, /* @__PURE__ */ React.createElement(
          StatusIndicator,
          {
            status: "error",
            message: attachment.error,
            tooltipProps: { enterDelay: 300 }
          }
        ));
      }
      default:
        return null;
    }
  })(), /* @__PURE__ */ React.createElement(
    RemoveButton,
    {
      label: t("HtmlPlugin_AiWidget_Remove_Image_Attachment_Button_AriaLabel"),
      onRemove: () => onRemove(attachment.id)
    }
  ));
};
const FileAttachment = ({ attachment, onRemove }) => {
  const { t } = useContext(RicosContext);
  const { name, type } = attachment.file;
  const getAttachmentIcon = (fileType) => {
    switch (fileType) {
      case "csv":
        return /* @__PURE__ */ React.createElement(DataCollection, { color: "#fff" });
      case "pdf":
        return /* @__PURE__ */ React.createElement(Document, { color: "#fff" });
      case "text":
      default:
        return /* @__PURE__ */ React.createElement(Document, { color: "#000" });
    }
  };
  const getIconBackground = () => {
    switch (attachment.status) {
      case "ok":
        if (type === "csv") {
          return "#25A55A";
        }
        if (type === "pdf") {
          return "#E62214";
        }
        return "#ECEFF3";
      case "loading":
        return "#ECEFF3";
      case "error":
        return "#fde3e1";
      default:
        unreachable(attachment);
        return null;
    }
  };
  return /* @__PURE__ */ React.createElement("div", { className: classes.attachmentWrapper }, /* @__PURE__ */ React.createElement(
    "div",
    {
      className: classNames(classes.fileAttachment, {
        [classes.error]: attachment.status === "error"
      }),
      style: {
        "--ricos-internal-file-attachment-background-color": getIconBackground()
      }
    },
    /* @__PURE__ */ React.createElement("div", { className: classes.fileAttacmentIcon }, (() => {
      switch (attachment.status) {
        case "ok":
          return getAttachmentIcon(type);
        case "loading":
          return /* @__PURE__ */ React.createElement(Loader, { size: "tiny" });
        case "error":
          return /* @__PURE__ */ React.createElement(
            StatusIndicator,
            {
              status: "error",
              message: attachment.error,
              tooltipProps: { enterDelay: 300 }
            }
          );
        default:
          unreachable(attachment);
          return null;
      }
    })()),
    /* @__PURE__ */ React.createElement(Box, { direction: "vertical", width: 137 }, /* @__PURE__ */ React.createElement(Text, { size: "small", weight: "normal", ellipsis: true, enterDelay: 300 }, name), /* @__PURE__ */ React.createElement(
      Text,
      {
        size: "tiny",
        weight: "thin",
        ellipsis: true,
        enterDelay: 300,
        skin: attachment.status === "error" ? "error" : void 0
      },
      (() => {
        switch (attachment.status) {
          case "ok":
          case "loading":
            return type.toUpperCase();
          case "error":
            return t("UploadFile_Error_Generic_Toast");
          default:
            unreachable(attachment);
            return null;
        }
      })()
    ))
  ), /* @__PURE__ */ React.createElement(
    RemoveButton,
    {
      label: t("HtmlPlugin_AiWidget_Remove_Attachment_Button_AriaLabel", { fileName: name }),
      onRemove: () => onRemove(attachment.id)
    }
  ));
};
const RemoveButton = ({ label, onRemove }) => /* @__PURE__ */ React.createElement("div", { className: classes.removeButton }, /* @__PURE__ */ React.createElement(CloseButton, { "aria-label": label, size: "small", skin: "standardFilled", onClick: onRemove }));
export {
  AttachmentBar
};
//# sourceMappingURL=attachment-bar.js.map
