"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.CompositeItem = exports.CompositeGroup = exports.Composite = void 0;
exports.useCompositeState = useCompositeState;
var _element = require("@wordpress/element");
var _ = require("..");
var _store = require("../store");
var _compose = require("@wordpress/compose");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * Composite is a component that may contain navigable items represented by
 * CompositeItem. It's inspired by the WAI-ARIA Composite Role and implements
 * all the keyboard navigation mechanisms to ensure that there's only one
 * tab stop for the whole Composite element. This means that it can behave as
 * a roving tabindex or aria-activedescendant container.
 *
 * This file aims at providing components that are as close as possible to the
 * original `reakit`-based implementation (which was removed from the codebase),
 * although it is recommended that consumers of the package switch to the stable,
 * un-prefixed, `ariakit`-based version of `Composite`.
 *
 * @see https://ariakit.org/components/composite
 */

/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

// Legacy composite components can either provide state through a
// single `state` prop, or via individual props, usually through
// spreading the state generated by `useCompositeState`.
// That is, `<Composite* { ...state }>`.

function mapLegacyStatePropsToComponentProps(legacyProps) {
  // If a `state` prop is provided, we unpack that; otherwise,
  // the necessary props are provided directly in `legacyProps`.
  if (legacyProps.state) {
    const {
      state,
      ...rest
    } = legacyProps;
    const {
      store,
      ...props
    } = mapLegacyStatePropsToComponentProps(state);
    return {
      ...rest,
      ...props,
      store
    };
  }
  return legacyProps;
}
function proxyComposite(ProxiedComponent, propMap = {}) {
  const displayName = ProxiedComponent.displayName;
  const Component = legacyProps => {
    const {
      store,
      ...rest
    } = mapLegacyStatePropsToComponentProps(legacyProps);
    const props = rest;
    props.id = (0, _compose.useInstanceId)(store, props.baseId, props.id);
    Object.entries(propMap).forEach(([from, to]) => {
      if (props.hasOwnProperty(from)) {
        Object.assign(props, {
          [to]: props[from]
        });
        delete props[from];
      }
    });
    delete props.baseId;
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(ProxiedComponent, {
      ...props,
      store: store
    });
  };
  Component.displayName = displayName;
  return Component;
}

// The old `CompositeGroup` used to behave more like the current
// `CompositeRow`, but this has been split into two different
// components. We handle that difference by checking on the
// provided role, and returning the appropriate component.
const unproxiedCompositeGroup = (0, _element.forwardRef)(({
  role,
  ...props
}, ref) => {
  const Component = role === 'row' ? _.Composite.Row : _.Composite.Group;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
    ref: ref,
    role: role,
    ...props
  });
});
const Composite = exports.Composite = proxyComposite(_.Composite, {
  baseId: 'id'
});
const CompositeGroup = exports.CompositeGroup = proxyComposite(unproxiedCompositeGroup);
const CompositeItem = exports.CompositeItem = proxyComposite(_.Composite.Item, {
  focusable: 'accessibleWhenDisabled'
});
function useCompositeState(legacyStateOptions = {}) {
  const {
    baseId,
    currentId: defaultActiveId,
    orientation,
    rtl = false,
    loop: focusLoop = false,
    wrap: focusWrap = false,
    shift: focusShift = false,
    // eslint-disable-next-line camelcase
    unstable_virtual: virtualFocus
  } = legacyStateOptions;
  return {
    baseId: (0, _compose.useInstanceId)(Composite, 'composite', baseId),
    store: (0, _store.useCompositeStore)({
      defaultActiveId,
      rtl,
      orientation,
      focusLoop,
      focusShift,
      focusWrap,
      virtualFocus
    })
  };
}
//# sourceMappingURL=index.js.map