import { jsxDEV, Fragment as Fragment$1 } from "react/jsx-dev-runtime";
import { Component, useMutationState, useSortedEntities, Entity, SugaredField } from "@contember/binding";
import { useCallback, Fragment } from "react";
import { repeaterDictionary } from "./repeaterDictionary.js";
import { RepeaterFieldContainer } from "./RepeaterFieldContainer.js";
import { RepeaterItem } from "./RepeaterItem.js";
import { shouldCancelStart } from "../../helpers/shouldCancelStart.js";
import { SortableRepeaterContainer } from "./SortableRepeaterContainer.js";
import { SortableRepeaterItem } from "./SortableRepeaterItem.js";
import { SortableRepeaterItemHandle } from "./SortableRepeaterItemHandle.js";
import { useMessageFormatter } from "../../../../i18n/useMessageFormatter.js";
const RepeaterInner = Component(
  (props) => {
    const isMutating = useMutationState();
    const { entities, moveEntity, addNewAtIndex } = useSortedEntities(props.accessor, props.sortableBy);
    const createNewEntity = useCallback((initialize, index) => {
      addNewAtIndex(index, initialize);
    }, [addNewAtIndex]);
    const onSortEnd = useCallback(
      ({ oldIndex, newIndex }) => {
        moveEntity(oldIndex, newIndex);
      },
      [moveEntity]
    );
    const formatMessage = useMessageFormatter(repeaterDictionary);
    const Handle = props.dragHandleComponent || Fragment;
    const Item = props.itemComponent || RepeaterItem;
    const Container = props.containerComponent || RepeaterFieldContainer;
    const isEmpty = entities.length === 0;
    const itemRemovingEnabled = props.enableRemoving !== false;
    const sortableHandle = useCallback(
      ({ children }) => /* @__PURE__ */ jsxDEV(SortableRepeaterItemHandle, { children: /* @__PURE__ */ jsxDEV(Handle, { children }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/Repeater/RepeaterInner.tsx",
        lineNumber: 88,
        columnNumber: 6
      }, globalThis) }, void 0, false, {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/Repeater/RepeaterInner.tsx",
        lineNumber: 87,
        columnNumber: 5
      }, globalThis),
      [Handle]
    );
    const removalType = props.removalType ?? "delete";
    const label = props.label ?? props.boxLabel;
    if (props.sortableBy === void 0) {
      return /* @__PURE__ */ jsxDEV(
        Container,
        {
          ...props.containerComponentExtraProps,
          ...props,
          removalType,
          isEmpty,
          createNewEntity,
          entities,
          formatMessage,
          children: entities.map((entity, i) => /* @__PURE__ */ jsxDEV(Entity, { accessor: entity, children: /* @__PURE__ */ 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: 110,
              columnNumber: 8
            },
            globalThis
          ) }, entity.key, false, {
            fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/Repeater/RepeaterInner.tsx",
            lineNumber: 109,
            columnNumber: 7
          }, globalThis))
        },
        void 0,
        false,
        {
          fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/Repeater/RepeaterInner.tsx",
          lineNumber: 99,
          columnNumber: 5
        },
        globalThis
      );
    }
    const axis = props.unstable__sortAxis || "y";
    const useDragHandle = props.useDragHandle ?? true;
    return /* @__PURE__ */ jsxDEV(
      SortableRepeaterContainer,
      {
        axis,
        lockAxis: axis,
        helperClass: "is-active",
        lockToContainerEdges: true,
        useWindowAsScrollContainer: true,
        useDragHandle,
        onSortEnd,
        shouldCancelStart,
        children: /* @__PURE__ */ jsxDEV(
          Container,
          {
            ...props.containerComponentExtraProps,
            ...props,
            removalType,
            isEmpty,
            createNewEntity,
            entities,
            formatMessage,
            children: entities.map((entity, i) => /* @__PURE__ */ jsxDEV(SortableRepeaterItem, { index: i, disabled: isMutating, children: /* @__PURE__ */ jsxDEV(Entity, { accessor: entity, children: /* @__PURE__ */ 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: 153,
                columnNumber: 9
              },
              globalThis
            ) }, void 0, false, {
              fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/Repeater/RepeaterInner.tsx",
              lineNumber: 152,
              columnNumber: 8
            }, globalThis) }, entity.key, false, {
              fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/Repeater/RepeaterInner.tsx",
              lineNumber: 151,
              columnNumber: 7
            }, globalThis))
          },
          void 0,
          false,
          {
            fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/Repeater/RepeaterInner.tsx",
            lineNumber: 141,
            columnNumber: 5
          },
          globalThis
        )
      },
      void 0,
      false,
      {
        fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/Repeater/RepeaterInner.tsx",
        lineNumber: 131,
        columnNumber: 4
      },
      globalThis
    );
  },
  (props) => /* @__PURE__ */ jsxDEV(Fragment$1, { children: [
    props.sortableBy && /* @__PURE__ */ jsxDEV(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: 175,
      columnNumber: 25
    }, globalThis),
    props.children
  ] }, void 0, true, {
    fileName: "/__w/interface/interface/packages/admin/src/components/bindingFacade/collections/Repeater/RepeaterInner.tsx",
    lineNumber: 174,
    columnNumber: 3
  }, globalThis),
  "RepeaterInner"
);
export {
  RepeaterInner
};
//# sourceMappingURL=RepeaterInner.js.map
