"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.SortableList = void 0;
var _core = require("@dnd-kit/core");
var _sortable = require("@dnd-kit/sortable");
var _react = require("react");
var _i18n = require("../../../i18n.generated/i18n.cjs");
var _SortableItem = require("./SortableItem.cjs");
var _SortableOverlay = require("./SortableOverlay.cjs");
var _jsxRuntime = require("react/jsx-runtime");
/*!
 * 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.
 */

const SortableList = ({
  parentId,
  items,
  onChange,
  renderItem
}) => {
  const [active, setActive] = (0, _react.useState)(null);
  const activeItem = (0, _react.useMemo)(() => items.find(item => item.id === active?.id), [active, items]);
  const sensors = (0, _core.useSensors)((0, _core.useSensor)(_core.PointerSensor), (0, _core.useSensor)(_core.KeyboardSensor, {
    coordinateGetter: _sortable.sortableKeyboardCoordinates
  }));
  const {
    t
  } = (0, _i18n.useTranslation)();
  const announcements = (0, _react.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 (0, _jsxRuntime.jsxs)(_core.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: [(0, _jsxRuntime.jsx)(_sortable.SortableContext, {
      items: items,
      children: items.map(item => (0, _jsxRuntime.jsx)(_react.Fragment, {
        children: renderItem(item)
      }, item.id))
    }), (0, _jsxRuntime.jsx)(_SortableOverlay.SortableOverlay, {
      children: activeItem ? renderItem(activeItem) : null
    })]
  });
};
exports.SortableList = SortableList;
SortableList.Item = _SortableItem.SortableItem;
//# sourceMappingURL=SortableList.cjs.map