"use strict";

exports.__esModule = true;
exports.DragAndDropAnnouncements = void 0;
var _reactDom = require("react-dom");
var _react = _interopRequireWildcard(require("react"));
var _mobxReactLite = require("mobx-react-lite");
var _VisuallyHidden = require("../VisuallyHidden");
var _react2 = require("@wix/bex-core/react");
var _mobx = require("mobx");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/components/DragAndDrop/DragAndDropAnnouncements.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _DragAndDropAnnouncements(props) {
  const {
    state,
    dataHook
  } = props;
  const {
    modalsContainerRef
  } = (0, _react2.usePageContext)();
  const {
    environment: {
      language
    },
    translate: t
  } = (0, _react2.useWixPatternsContainer)();
  const {
    collection: {
      total
    }
  } = state;
  const [localState, setLocalState] = (0, _react.useState)(() => (0, _mobx.runInAction)(() => {
    const current = {
      status: state.status,
      active: state.active,
      over: state.over
    };
    return {
      current,
      prev: null
    };
  }));
  (0, _react.useEffect)(() => (0, _mobx.reaction)(() => ({
    status: state.status,
    active: state.active,
    over: state.over
  }), (current, prev) => {
    setLocalState({
      current,
      prev
    });
  }, {
    fireImmediately: true
  }), []);
  const {
    over,
    status,
    active
  } = localState.current;
  const announcement = (_localState$prev => {
    switch (status) {
      case 'start':
        if (active) {
          return t('cairo.dragAndDrop.live-region.itemGrabbed.sr');
        }
        return;
      case 'over':
        if (over && ((_localState$prev = localState.prev) == null ? void 0 : _localState$prev.status) !== 'start') {
          return t('cairo.dragAndDrop.live-region.itemDragged.sr', {
            currentNumber: over.index + 1,
            totalNumber: total
          });
        }
        if (active) {
          return t('cairo.dragAndDrop.live-region.itemGrabbed.sr');
        }
        return;
      case 'end':
        if (over) {
          var _localState$prev2;
          if (over.id === (active == null ? void 0 : active.id)) {
            return t('cairo.dragAndDrop.reorder-cancelled.sr');
          }
          const activeOrPrev = active || ((_localState$prev2 = localState.prev) == null ? void 0 : _localState$prev2.active);
          if (activeOrPrev) {
            return t('cairo.dragAndDrop.live-region.itemDropped.sr', {
              currentNumber: activeOrPrev.index + 1
            });
          }
          return;
        }
        return;
      case 'cancel':
        return t('cairo.dragAndDrop.reorder-cancelled.sr');
    }
    return;
  })();
  let child = /*#__PURE__*/_react.default.createElement("div", {
    "data-hook": dataHook,
    style: _VisuallyHidden.visuallyHidden,
    role: "status",
    "aria-live": "assertive",
    "aria-atomic": true,
    lang: language,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 112,
      columnNumber: 5
    }
  }, announcement);
  if (!modalsContainerRef.current) {
    return null;
  }
  child = /*#__PURE__*/(0, _reactDom.createPortal)(child, modalsContainerRef.current);
  return child;
}
const DragAndDropAnnouncements = exports.DragAndDropAnnouncements = (0, _mobxReactLite.observer)(_DragAndDropAnnouncements);
//# sourceMappingURL=DragAndDropAnnouncements.js.map