"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.ZStack = void 0;
var _element = require("@wordpress/element");
var _getValidChildren = require("../utils/get-valid-children");
var _context = require("../context");
var _styles = require("./styles");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * External dependencies
 */

/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

function UnconnectedZStack(props, forwardedRef) {
  const {
    children,
    className,
    isLayered = true,
    isReversed = false,
    offset = 0,
    ...otherProps
  } = (0, _context.useContextSystem)(props, 'ZStack');
  const validChildren = (0, _getValidChildren.getValidChildren)(children);
  const childrenLastIndex = validChildren.length - 1;
  const clonedChildren = validChildren.map((child, index) => {
    const zIndex = isReversed ? childrenLastIndex - index : index;
    // Only when the component is layered, the offset needs to be multiplied by
    // the item's index, so that items can correctly stack at the right distance
    const offsetAmount = isLayered ? offset * index : offset;
    const key = (0, _element.isValidElement)(child) ? child.key : index;
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_styles.ZStackChildView, {
      offsetAmount: offsetAmount,
      zIndex: zIndex,
      children: child
    }, key);
  });
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_styles.ZStackView, {
    ...otherProps,
    className: className,
    isLayered: isLayered,
    ref: forwardedRef,
    children: clonedChildren
  });
}

/**
 * `ZStack` allows you to stack things along the Z-axis.
 *
 * ```jsx
 * import { __experimentalZStack as ZStack } from '@wordpress/components';
 *
 * function Example() {
 *   return (
 *     <ZStack offset={ 20 } isLayered>
 *       <ExampleImage />
 *       <ExampleImage />
 *       <ExampleImage />
 *     </ZStack>
 *   );
 * }
 * ```
 */
const ZStack = exports.ZStack = (0, _context.contextConnect)(UnconnectedZStack, 'ZStack');
var _default = exports.default = ZStack;
//# sourceMappingURL=component.js.map