"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-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__ */ jsxRuntime.jsx(InitializingFilePreview.InitializingFilePreview, { formatMessage });
      }
      if (uploadState.readyState === "error" && resolvedEntity.fileKind === void 0) {
        preview = /* @__PURE__ */ jsxRuntime.jsx(ErrorFilePreview.ErrorFilePreview, { uploadState, formatMessage });
      } else {
        if (resolvedEntity.fileKind === void 0) {
          throw new reactBinding.BindingError();
        }
        preview = /* @__PURE__ */ jsxRuntime.jsx(
          InitializedFilePreview.InitializedFilePreview,
          {
            fileKind: resolvedEntity.fileKind,
            formatMessage,
            resolvedEntity,
            uploadState
          }
        );
      }
    } else if (!resolvedEntity.isEmpty) {
      preview = /* @__PURE__ */ jsxRuntime.jsx(UploadedFilePreview.UploadedFilePreview, { fileKind: resolvedEntity.fileKind });
    } else {
      return null;
    }
    let editContents = resolvedEntity.fileKind && resolvedEntity.fileKind.children ? resolvedEntity.fileKind.children : void 0;
    if (resolvedEntity.fileKind?.childrenOutsideBaseEntity) {
      editContents = /* @__PURE__ */ jsxRuntime.jsx(reactBinding.Entity, { accessor: resolvedEntity.parentEntity, children: editContents });
    }
    const box = /* @__PURE__ */ jsxRuntime.jsx(ui.ActionableBox, { className: ui.toEnumViewClass(viewFromMimeType(resolvedEntity.fileKind?.acceptMimeTypes)), onRemove, editContents, children: preview });
    return resolvedEntity.fileEntity ? /* @__PURE__ */ jsxRuntime.jsx(reactBinding.Entity, { accessor: resolvedEntity.fileEntity, children: box }) : box;
  }
);
SingleFilePreview.displayName = "SingleFilePreview";
exports.SingleFilePreview = SingleFilePreview;
//# sourceMappingURL=SingleFilePreview.cjs.map
