/*!
 * Copyright (c) 2024-present, Okta, Inc. and/or its affiliates. All rights reserved.
 * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
 *
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *
 * See the License for the specific language governing permissions and limitations under the License.
 */

import { DndContext, KeyboardSensor, PointerSensor, useSensor, useSensors } from "@dnd-kit/core";
import { SortableContext, sortableKeyboardCoordinates } from "@dnd-kit/sortable";
import { Fragment, useMemo, useState } from "react";
import { useTranslation } from "../../../i18n.generated/i18n.js";
import { SortableItem } from "./SortableItem.js";
import { SortableOverlay } from "./SortableOverlay.js";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export const SortableList = ({
  parentId,
  items,
  onChange,
  renderItem
}) => {
  const [active, setActive] = useState(null);
  const activeItem = useMemo(() => items.find(item => item.id === active?.id), [active, items]);
  const sensors = useSensors(useSensor(PointerSensor), useSensor(KeyboardSensor, {
    coordinateGetter: sortableKeyboardCoordinates
  }));
  const {
    t
  } = useTranslation();
  const announcements = useMemo(() => ({
    onDragStart: ({
      active
    }) => {
      return `${t("sortable.list.drag.start", {
        activeId: active.id
      })}`;
    },
    onDragOver: ({
      active,
      over
    }) => {
      if (over) {
        return `${t("sortable.list.drag.moved.over", {
          activeId: active.id,
          overId: over.id
        })}`;
      }
      return `${t("sortable.list.drag.nolonger.over", {
        activeId: active.id
      })}`;
    },
    onDragEnd: ({
      active,
      over
    }) => {
      if (over) {
        return `${t("sortable.list.drag.end.dropped.over", {
          activeId: active.id,
          overId: over.id
        })}`;
      }
      return `${t("sortable.list.drag.end.dropped", {
        activeId: active.id
      })}`;
    },
    onDragCancel: ({
      active
    }) => {
      return `${t("sortable.list.drag.cancel", {
        activeId: active.id
      })}`;
    }
  }), [t]);
  return _jsxs(DndContext, {
    accessibility: {
      announcements: announcements
    },
    onDragCancel: () => {
      setActive(null);
    },
    onDragEnd: ({
      active,
      over
    }) => {
      if (over && active.id !== over?.id) {
        const activeIndex = items.findIndex(({
          id
        }) => id === active.id);
        const overIndex = items.findIndex(({
          id
        }) => id === over.id);
        if (items[overIndex].isSortable !== false) {
          onChange(parentId, active.id, activeIndex, overIndex);
        }
      }
      setActive(null);
    },
    onDragStart: ({
      active
    }) => {
      setActive(active);
    },
    sensors: sensors,
    children: [_jsx(SortableContext, {
      items: items,
      children: items.map(item => _jsx(Fragment, {
        children: renderItem(item)
      }, item.id))
    }), _jsx(SortableOverlay, {
      children: activeItem ? renderItem(activeItem) : null
    })]
  });
};
SortableList.Item = SortableItem;
//# sourceMappingURL=SortableList.js.map