"use strict";
var __defProp = Object.defineProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __publicField = (obj, key, value) => {
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
  return value;
};
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxDevRuntime = require("react/jsx-dev-runtime");
const reactBinding = require("@contember/react-binding");
const resolveAccept = require("./utils/resolveAccept.cjs");
const resolveAllAcceptedMimes = require("./utils/resolveAllAcceptedMimes.cjs");
const react = require("react");
const disconnectAtBase = require("./utils/disconnectAtBase.cjs");
const staticRenderFileKind = require("./utils/staticRenderFileKind.cjs");
const AcceptFileKindError = require("./AcceptFileKindError.cjs");
class DiscriminatedFileHandler {
  constructor(discriminationField, baseEntity, fileKinds) {
    __publicField(this, "hasBaseEntity");
    __publicField(this, "acceptedMimes");
    this.discriminationField = discriminationField;
    this.baseEntity = baseEntity;
    this.fileKinds = fileKinds;
    this.hasBaseEntity = this.baseEntity !== void 0;
    this.acceptedMimes = resolveAllAcceptedMimes.resolveAllAcceptedMimes(Array.from(fileKinds.values(), (it) => it.datum.acceptMimeTypes).flat());
  }
  async acceptFile(fileOptions) {
    const errors = [];
    for (const { datum: fileKind, discriminateBy } of this.fileKinds.values()) {
      try {
        const result = await resolveAccept.resolveAcceptingSingleKind(fileOptions, fileKind);
        if (result !== void 0) {
          return {
            fileKind,
            finalizeEntity: (entity) => {
              DiscriminatedFileHandler.fillDiscriminateBy(entity, this.baseEntity, discriminateBy, this.discriminationField);
            }
          };
        }
      } catch (e) {
        if (e instanceof AcceptFileKindError.AcceptFileKindError) {
          errors.push(e);
        }
      }
    }
    if (errors.length) {
      throw new AggregateError(errors);
    } else {
      return void 0;
    }
  }
  resolveEntity(parentEntity) {
    const containingEntity = this.getContainingEntity(parentEntity);
    const fileKind = this.resolveFileKind(parentEntity);
    if (!fileKind) {
      return {
        parentEntity,
        isEmpty: true,
        fileEntity: void 0,
        fileKind: void 0,
        getErrorHolders: () => {
          return [
            parentEntity,
            ...parentEntity == containingEntity ? [containingEntity] : []
          ];
        }
      };
    }
    const fileEntity = fileKind.baseEntity ? containingEntity.getEntity(fileKind.baseEntity) : containingEntity;
    const baseEntity = this.baseEntity;
    return {
      parentEntity,
      isEmpty: false,
      fileEntity,
      fileKind,
      destroy: baseEntity ? () => {
        disconnectAtBase.disconnectAtBase(baseEntity, parentEntity);
      } : void 0,
      getErrorHolders: () => {
        return [
          parentEntity,
          ...containingEntity !== parentEntity ? [containingEntity] : [],
          ...fileEntity !== containingEntity ? [fileEntity] : [],
          ...fileKind.extractors.flatMap((it) => it.getErrorsHolders?.({
            entity: fileEntity,
            environment: fileEntity.environment
          }) ?? [])
        ];
      }
    };
  }
  staticRender(environment) {
    const staticKinds = Array.from(this.fileKinds.values(), (fileKind) => staticRenderFileKind.staticRenderFileKind(fileKind.datum, environment));
    const childrenOutsideBase = /* @__PURE__ */ jsxDevRuntime.jsxDEV(jsxDevRuntime.Fragment, { children: staticKinds.map(([, it], index) => /* @__PURE__ */ jsxDevRuntime.jsxDEV(react.Fragment, { children: it }, index, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/fileHandler/DiscriminatedFileHandler.tsx",
      lineNumber: 108,
      columnNumber: 40
    }, this)) }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/fileHandler/DiscriminatedFileHandler.tsx",
      lineNumber: 107,
      columnNumber: 31
    }, this);
    const children = /* @__PURE__ */ jsxDevRuntime.jsxDEV(jsxDevRuntime.Fragment, { children: [
      /* @__PURE__ */ jsxDevRuntime.jsxDEV(reactBinding.SugaredField, { field: this.discriminationField, isNonbearing: true }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/fileHandler/DiscriminatedFileHandler.tsx",
        lineNumber: 112,
        columnNumber: 5
      }, this),
      staticKinds.map(([children2], i) => /* @__PURE__ */ jsxDevRuntime.jsxDEV(react.Fragment, { children: children2 }, i, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/fileHandler/DiscriminatedFileHandler.tsx",
        lineNumber: 114,
        columnNumber: 6
      }, this))
    ] }, void 0, true, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/fileHandler/DiscriminatedFileHandler.tsx",
      lineNumber: 111,
      columnNumber: 4
    }, this);
    return this.baseEntity === void 0 ? /* @__PURE__ */ jsxDevRuntime.jsxDEV(jsxDevRuntime.Fragment, { children: [
      children,
      childrenOutsideBase
    ] }, void 0, true, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/fileHandler/DiscriminatedFileHandler.tsx",
      lineNumber: 119,
      columnNumber: 6
    }, this) : /* @__PURE__ */ jsxDevRuntime.jsxDEV(jsxDevRuntime.Fragment, { children: [
      childrenOutsideBase,
      /* @__PURE__ */ jsxDevRuntime.jsxDEV(reactBinding.HasOne, { field: this.baseEntity, children }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/fileHandler/DiscriminatedFileHandler.tsx",
        lineNumber: 125,
        columnNumber: 5
      }, this)
    ] }, void 0, true, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/upload/fileHandler/DiscriminatedFileHandler.tsx",
      lineNumber: 123,
      columnNumber: 6
    }, this);
  }
  resolveFileKind(parent) {
    const containingEntity = this.getContainingEntity(parent);
    const discriminant = containingEntity.getField(this.discriminationField).value;
    if (!discriminant) {
      return void 0;
    }
    const fileKind = this.fileKinds.get(discriminant);
    if (!fileKind) {
      throw new reactBinding.BindingError(`Undefined file kind "${discriminant}".`);
    }
    return fileKind.datum;
  }
  getContainingEntity(accessor) {
    return this.baseEntity ? accessor.getEntity(this.baseEntity) : accessor;
  }
  static fillDiscriminateBy(entity, baseEntity, discriminateBy, discriminationField) {
    if (baseEntity !== void 0) {
      entity = entity.getEntity(baseEntity);
    }
    entity.getField(discriminationField).updateValue(
      reactBinding.VariableInputTransformer.transformValue(discriminateBy, entity.environment)
    );
  }
}
exports.DiscriminatedFileHandler = DiscriminatedFileHandler;
//# sourceMappingURL=DiscriminatedFileHandler.cjs.map
