"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxDevRuntime = require("react/jsx-dev-runtime");
const reactBinding = require("@contember/react-binding");
const react = require("react");
const reactI18n = require("@contember/react-i18n");
const RepeaterFieldContainer = require("./RepeaterFieldContainer.cjs");
const RepeaterItem = require("./RepeaterItem.cjs");
const SortableRepeaterContainer = require("./SortableRepeaterContainer.cjs");
const SortableRepeaterItem = require("./SortableRepeaterItem.cjs");
const SortableRepeaterItemHandle = require("./SortableRepeaterItemHandle.cjs");
const repeaterDictionary = require("./repeaterDictionary.cjs");
const utilities = require("@contember/utilities");
const RepeaterInner = reactBinding.Component(
  (props) => {
    const isMutating = reactBinding.useMutationState();
    const { entities, moveEntity, addNewAtIndex } = reactBinding.useSortedEntities(props.accessor, props.sortableBy);
    const createNewEntity = react.useCallback((initialize, index) => {
      addNewAtIndex(index, initialize);
    }, [addNewAtIndex]);
    const onSortEnd = react.useCallback(
      ({ oldIndex, newIndex }) => {
        moveEntity(oldIndex, newIndex);
      },
      [moveEntity]
    );
    const formatMessage = reactI18n.useMessageFormatter(repeaterDictionary.repeaterDictionary);
    const Handle = props.dragHandleComponent || react.Fragment;
    const Item = props.itemComponent || RepeaterItem.RepeaterItem;
    const Container = props.containerComponent || RepeaterFieldContainer.RepeaterFieldContainer;
    const isEmpty = entities.length === 0;
    const itemRemovingEnabled = props.enableRemoving !== false;
    const sortableHandle = react.useCallback(
      ({ children }) => /* @__PURE__ */ jsxDevRuntime.jsxDEV(SortableRepeaterItemHandle.SortableRepeaterItemHandle, { children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(Handle, { children }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/Repeater/RepeaterInner.tsx",
        lineNumber: 84,
        columnNumber: 6
      }, void 0) }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/Repeater/RepeaterInner.tsx",
        lineNumber: 83,
        columnNumber: 5
      }, void 0),
      [Handle]
    );
    const removalType = props.removalType ?? "delete";
    const label = props.label;
    if (props.sortableBy === void 0) {
      return /* @__PURE__ */ jsxDevRuntime.jsxDEV(
        Container,
        {
          ...props.containerComponentExtraProps,
          ...props,
          removalType,
          isEmpty,
          createNewEntity,
          entities,
          formatMessage,
          children: entities.map((entity, i) => /* @__PURE__ */ jsxDevRuntime.jsxDEV(reactBinding.Entity, { accessor: entity, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(
            Item,
            {
              ...props.itemComponentExtraProps,
              label,
              index: i,
              createNewEntity,
              removalType,
              canBeRemoved: itemRemovingEnabled,
              dragHandleComponent: void 0,
              children: props.children
            },
            void 0,
            false,
            {
              fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/Repeater/RepeaterInner.tsx",
              lineNumber: 106,
              columnNumber: 8
            },
            void 0
          ) }, entity.key, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/Repeater/RepeaterInner.tsx",
            lineNumber: 105,
            columnNumber: 7
          }, void 0))
        },
        void 0,
        false,
        {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/Repeater/RepeaterInner.tsx",
          lineNumber: 95,
          columnNumber: 5
        },
        void 0
      );
    }
    const axis = props.unstable__sortAxis || "y";
    const useDragHandle = props.useDragHandle ?? true;
    return /* @__PURE__ */ jsxDevRuntime.jsxDEV(
      SortableRepeaterContainer.SortableRepeaterContainer,
      {
        axis,
        lockAxis: axis,
        helperClass: "is-dragged",
        lockToContainerEdges: true,
        useWindowAsScrollContainer: true,
        useDragHandle,
        onSortEnd,
        shouldCancelStart: utilities.shouldCancelStart,
        children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(
          Container,
          {
            ...props.containerComponentExtraProps,
            ...props,
            removalType,
            isEmpty,
            createNewEntity,
            entities,
            formatMessage,
            children: entities.map((entity, i) => /* @__PURE__ */ jsxDevRuntime.jsxDEV(SortableRepeaterItem.SortableRepeaterItem, { index: i, disabled: isMutating, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(reactBinding.Entity, { accessor: entity, children: /* @__PURE__ */ jsxDevRuntime.jsxDEV(
              Item,
              {
                ...props.itemComponentExtraProps,
                label,
                index: i,
                createNewEntity,
                removalType,
                canBeRemoved: itemRemovingEnabled,
                dragHandleComponent: useDragHandle ? sortableHandle : void 0,
                children: props.children
              },
              void 0,
              false,
              {
                fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/Repeater/RepeaterInner.tsx",
                lineNumber: 149,
                columnNumber: 9
              },
              void 0
            ) }, void 0, false, {
              fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/Repeater/RepeaterInner.tsx",
              lineNumber: 148,
              columnNumber: 8
            }, void 0) }, entity.key, false, {
              fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/Repeater/RepeaterInner.tsx",
              lineNumber: 147,
              columnNumber: 7
            }, void 0))
          },
          void 0,
          false,
          {
            fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/Repeater/RepeaterInner.tsx",
            lineNumber: 137,
            columnNumber: 5
          },
          void 0
        )
      },
      void 0,
      false,
      {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/Repeater/RepeaterInner.tsx",
        lineNumber: 127,
        columnNumber: 4
      },
      void 0
    );
  },
  (props) => /* @__PURE__ */ jsxDevRuntime.jsxDEV(jsxDevRuntime.Fragment, { children: [
    props.sortableBy && /* @__PURE__ */ jsxDevRuntime.jsxDEV(reactBinding.SugaredField, { field: props.sortableBy, defaultValue: 0, isNonbearing: true }, void 0, false, {
      fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/Repeater/RepeaterInner.tsx",
      lineNumber: 171,
      columnNumber: 25
    }, void 0),
    props.children
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/Repeater/RepeaterInner.tsx",
    lineNumber: 170,
    columnNumber: 3
  }, void 0),
  "RepeaterInner"
);
exports.RepeaterInner = RepeaterInner;
//# sourceMappingURL=RepeaterInner.cjs.map
