"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxDevRuntime = require("react/jsx-dev-runtime");
const reactBinding = require("@contember/react-binding");
const ui = require("@contember/ui");
const react = require("react");
const ErrorFilePreview = require("./ErrorFilePreview.cjs");
const InitializedFilePreview = require("./InitializedFilePreview.cjs");
const InitializingFilePreview = require("./InitializingFilePreview.cjs");
const UploadedFilePreview = require("./UploadedFilePreview.cjs");
const viewFromMimeTypeRegExp = new RegExp(/^(\w+).*/);
function viewFromMimeType(mimeType) {
  if (!mimeType)
    return;
  return (Array.isArray(mimeType) ? mimeType[0] : mimeType).replace(viewFromMimeTypeRegExp, "$1");
}
const SingleFilePreview = react.memo(
  ({ fileId, resolvedEntity, formatMessage, removeFile, uploadState }) => {
    let preview = null;
    const onRemove = react.useMemo(() => {
      if (!removeFile) {
        return void 0;
      }
      return () => {
        removeFile(fileId);
      };
    }, [fileId, removeFile]);
    if (uploadState !== void 0) {
      if (uploadState.readyState === "initializing") {
        return /* @__PURE__ */ jsxDevRuntime.jsxDEV(InitializingFilePreview.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__ */ jsxDevRuntime.jsxDEV(ErrorFilePreview.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 reactBinding.BindingError();
        }
        preview = /* @__PURE__ */ jsxDevRuntime.jsxDEV(
          InitializedFilePreview.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__ */ jsxDevRuntime.jsxDEV(UploadedFilePreview.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__ */ jsxDevRuntime.jsxDEV(reactBinding.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__ */ jsxDevRuntime.jsxDEV(ui.ActionableBox, { className: ui.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__ */ jsxDevRuntime.jsxDEV(reactBinding.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";
exports.SingleFilePreview = SingleFilePreview;
//# sourceMappingURL=SingleFilePreview.cjs.map
