"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-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__ */ jsxRuntime.jsx(SortableRepeaterItemHandle.SortableRepeaterItemHandle, { children: /* @__PURE__ */ jsxRuntime.jsx(Handle, { children }) }),
      [Handle]
    );
    const removalType = props.removalType ?? "delete";
    const label = props.label;
    if (props.sortableBy === void 0) {
      return /* @__PURE__ */ jsxRuntime.jsx(
        Container,
        {
          ...props.containerComponentExtraProps,
          ...props,
          removalType,
          isEmpty,
          createNewEntity,
          entities,
          formatMessage,
          children: entities.map((entity, i) => /* @__PURE__ */ jsxRuntime.jsx(reactBinding.Entity, { accessor: entity, children: /* @__PURE__ */ jsxRuntime.jsx(
            Item,
            {
              ...props.itemComponentExtraProps,
              label,
              index: i,
              createNewEntity,
              removalType,
              canBeRemoved: itemRemovingEnabled,
              dragHandleComponent: void 0,
              children: props.children
            }
          ) }, entity.key))
        }
      );
    }
    const axis = props.unstable__sortAxis || "y";
    const useDragHandle = props.useDragHandle ?? true;
    return /* @__PURE__ */ jsxRuntime.jsx(
      SortableRepeaterContainer.SortableRepeaterContainer,
      {
        axis,
        lockAxis: axis,
        helperClass: "is-dragged",
        lockToContainerEdges: true,
        useWindowAsScrollContainer: true,
        useDragHandle,
        onSortEnd,
        shouldCancelStart: utilities.shouldCancelStart,
        children: /* @__PURE__ */ jsxRuntime.jsx(
          Container,
          {
            ...props.containerComponentExtraProps,
            ...props,
            removalType,
            isEmpty,
            createNewEntity,
            entities,
            formatMessage,
            children: entities.map((entity, i) => /* @__PURE__ */ jsxRuntime.jsx(SortableRepeaterItem.SortableRepeaterItem, { index: i, disabled: isMutating, children: /* @__PURE__ */ jsxRuntime.jsx(reactBinding.Entity, { accessor: entity, children: /* @__PURE__ */ jsxRuntime.jsx(
              Item,
              {
                ...props.itemComponentExtraProps,
                label,
                index: i,
                createNewEntity,
                removalType,
                canBeRemoved: itemRemovingEnabled,
                dragHandleComponent: useDragHandle ? sortableHandle : void 0,
                children: props.children
              }
            ) }) }, entity.key))
          }
        )
      }
    );
  },
  (props) => /* @__PURE__ */ jsxRuntime.jsxs(jsxRuntime.Fragment, { children: [
    props.sortableBy && /* @__PURE__ */ jsxRuntime.jsx(reactBinding.SugaredField, { field: props.sortableBy, defaultValue: 0, isNonbearing: true }),
    props.children
  ] }),
  "RepeaterInner"
);
exports.RepeaterInner = RepeaterInner;
//# sourceMappingURL=RepeaterInner.cjs.map
