import { jsxDEV, Fragment as Fragment$1 } from "react/jsx-dev-runtime";
import { Component, useMutationState, useSortedEntities, Entity, SugaredField } from "@contember/react-binding";
import { useCallback, Fragment } from "react";
import { useMessageFormatter } from "@contember/react-i18n";
import { RepeaterFieldContainer } from "./RepeaterFieldContainer.js";
import { RepeaterItem } from "./RepeaterItem.js";
import { SortableRepeaterContainer } from "./SortableRepeaterContainer.js";
import { SortableRepeaterItem } from "./SortableRepeaterItem.js";
import { SortableRepeaterItemHandle } from "./SortableRepeaterItemHandle.js";
import { repeaterDictionary } from "./repeaterDictionary.js";
import { shouldCancelStart } from "@contember/utilities";
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: 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__ */ 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: 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__ */ jsxDEV(
      SortableRepeaterContainer,
      {
        axis,
        lockAxis: axis,
        helperClass: "is-dragged",
        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: 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__ */ 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: 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"
);
export {
  RepeaterInner
};
//# sourceMappingURL=RepeaterInner.js.map
