import { jsxDEV } from "react/jsx-dev-runtime";
import { BindingError, Entity } from "@contember/react-binding";
import { ActionableBox, toEnumViewClass } from "@contember/ui";
import { memo, useMemo } from "react";
import { ErrorFilePreview } from "./ErrorFilePreview.js";
import { InitializedFilePreview } from "./InitializedFilePreview.js";
import { InitializingFilePreview } from "./InitializingFilePreview.js";
import { UploadedFilePreview } from "./UploadedFilePreview.js";
const viewFromMimeTypeRegExp = new RegExp(/^(\w+).*/);
function viewFromMimeType(mimeType) {
  if (!mimeType)
    return;
  return (Array.isArray(mimeType) ? mimeType[0] : mimeType).replace(viewFromMimeTypeRegExp, "$1");
}
const SingleFilePreview = memo(
  ({ fileId, resolvedEntity, formatMessage, removeFile, uploadState }) => {
    let preview = null;
    const onRemove = useMemo(() => {
      if (!removeFile) {
        return void 0;
      }
      return () => {
        removeFile(fileId);
      };
    }, [fileId, removeFile]);
    if (uploadState !== void 0) {
      if (uploadState.readyState === "initializing") {
        return /* @__PURE__ */ jsxDEV(InitializingFilePreview, { formatMessage }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/SingleFilePreview/SingleFilePreview.tsx",
          lineNumber: 45,
          columnNumber: 12
        }, void 0);
      }
      if (uploadState.readyState === "error" && resolvedEntity.fileKind === void 0) {
        preview = /* @__PURE__ */ jsxDEV(ErrorFilePreview, { uploadState, formatMessage }, void 0, false, {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/SingleFilePreview/SingleFilePreview.tsx",
          lineNumber: 48,
          columnNumber: 15
        }, void 0);
      } else {
        if (resolvedEntity.fileKind === void 0) {
          throw new BindingError();
        }
        preview = /* @__PURE__ */ jsxDEV(
          InitializedFilePreview,
          {
            fileKind: resolvedEntity.fileKind,
            formatMessage,
            resolvedEntity,
            uploadState
          },
          void 0,
          false,
          {
            fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/SingleFilePreview/SingleFilePreview.tsx",
            lineNumber: 54,
            columnNumber: 6
          },
          void 0
        );
      }
    } else if (!resolvedEntity.isEmpty) {
      preview = /* @__PURE__ */ jsxDEV(UploadedFilePreview, { fileKind: resolvedEntity.fileKind }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/SingleFilePreview/SingleFilePreview.tsx",
        lineNumber: 63,
        columnNumber: 14
      }, void 0);
    } else {
      return null;
    }
    let editContents = resolvedEntity.fileKind && resolvedEntity.fileKind.children ? resolvedEntity.fileKind.children : void 0;
    if (resolvedEntity.fileKind?.childrenOutsideBaseEntity) {
      editContents = /* @__PURE__ */ jsxDEV(Entity, { accessor: resolvedEntity.parentEntity, children: editContents }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/SingleFilePreview/SingleFilePreview.tsx",
        lineNumber: 69,
        columnNumber: 19
      }, void 0);
    }
    const box = /* @__PURE__ */ jsxDEV(ActionableBox, { className: toEnumViewClass(viewFromMimeType(resolvedEntity.fileKind?.acceptMimeTypes)), onRemove, editContents, children: preview }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/SingleFilePreview/SingleFilePreview.tsx",
      lineNumber: 73,
      columnNumber: 4
    }, void 0);
    return resolvedEntity.fileEntity ? /* @__PURE__ */ jsxDEV(Entity, { accessor: resolvedEntity.fileEntity, children: box }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/internalComponents/SingleFilePreview/SingleFilePreview.tsx",
      lineNumber: 79,
      columnNumber: 5
    }, void 0) : box;
  }
);
SingleFilePreview.displayName = "SingleFilePreview";
export {
  SingleFilePreview
};
//# sourceMappingURL=SingleFilePreview.js.map
