import { useCallback, Fragment, createElement } from "react";
import { Component, useMutationState, useSortedEntities, Entity, SugaredField } from "@contember/binding";
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, appendNew } = useSortedEntities(props.accessor, props.sortableBy);
  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__ */ createElement(SortableRepeaterItemHandle, null, /* @__PURE__ */ createElement(Handle, null, children)), [Handle]);
  const removalType = props.removalType ?? "delete";
  const label = props.label ?? props.boxLabel;
  if (props.sortableBy === void 0) {
    return /* @__PURE__ */ createElement(Container, {
      ...props.containerComponentExtraProps,
      ...props,
      removalType,
      isEmpty,
      createNewEntity: appendNew,
      entities,
      formatMessage
    }, entities.map((entity, i) => /* @__PURE__ */ createElement(Entity, {
      accessor: entity,
      key: entity.key
    }, /* @__PURE__ */ createElement(Item, {
      ...props.itemComponentExtraProps,
      label: label ? `${label} #${i + 1}` : `#${i + 1}`,
      removalType,
      canBeRemoved: itemRemovingEnabled,
      dragHandleComponent: void 0
    }, props.children))));
  }
  const axis = props.unstable__sortAxis || "y";
  const useDragHandle = props.useDragHandle ?? true;
  return /* @__PURE__ */ createElement(SortableRepeaterContainer, {
    axis,
    lockAxis: axis,
    helperClass: "is-active",
    lockToContainerEdges: true,
    useWindowAsScrollContainer: true,
    useDragHandle,
    onSortEnd,
    shouldCancelStart
  }, /* @__PURE__ */ createElement(Container, {
    ...props.containerComponentExtraProps,
    ...props,
    removalType,
    isEmpty,
    createNewEntity: appendNew,
    entities,
    formatMessage
  }, entities.map((entity, i) => /* @__PURE__ */ createElement(SortableRepeaterItem, {
    index: i,
    key: entity.key,
    disabled: isMutating
  }, /* @__PURE__ */ createElement(Entity, {
    accessor: entity
  }, /* @__PURE__ */ createElement(Item, {
    ...props.itemComponentExtraProps,
    label: label ? `${label} #${i + 1}` : `#${i + 1}`,
    removalType,
    canBeRemoved: itemRemovingEnabled,
    dragHandleComponent: useDragHandle ? sortableHandle : void 0
  }, props.children))))));
}, (props) => /* @__PURE__ */ createElement(Fragment, null, props.sortableBy && /* @__PURE__ */ createElement(SugaredField, {
  field: props.sortableBy,
  defaultValue: 0,
  isNonbearing: true
}), props.children), "RepeaterInner");
export { RepeaterInner };
//# sourceMappingURL=RepeaterInner.js.map
