"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var Ariakit = _interopRequireWildcard(require("@ariakit/react"));
var _framerMotion = require("framer-motion");
var _compose = require("@wordpress/compose");
var _element = require("@wordpress/element");
var _context = require("../../context");
var _context2 = require("../context");
var styles = _interopRequireWildcard(require("./styles"));
var _hooks = require("../../utils/hooks");
var _tooltip = _interopRequireDefault(require("../../tooltip"));
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/**
 * External dependencies
 */

/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  ButtonContentView,
  LabelView
} = styles;
const REDUCED_MOTION_TRANSITION_CONFIG = {
  duration: 0
};
const LAYOUT_ID = 'toggle-group-backdrop-shared-layout-id';
const WithToolTip = ({
  showTooltip,
  text,
  children
}) => {
  if (showTooltip && text) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_tooltip.default, {
      text: text,
      placement: "top",
      children: children
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
    children: children
  });
};
function ToggleGroupControlOptionBase(props, forwardedRef) {
  const shouldReduceMotion = (0, _compose.useReducedMotion)();
  const toggleGroupControlContext = (0, _context2.useToggleGroupControlContext)();
  const id = (0, _compose.useInstanceId)(ToggleGroupControlOptionBase, toggleGroupControlContext.baseId || 'toggle-group-control-option-base');
  const buttonProps = (0, _context.useContextSystem)({
    ...props,
    id
  }, 'ToggleGroupControlOptionBase');
  const {
    isBlock = false,
    isDeselectable = false,
    size = 'default'
  } = toggleGroupControlContext;
  const {
    className,
    isIcon = false,
    value,
    children,
    showTooltip = false,
    onFocus: onFocusProp,
    ...otherButtonProps
  } = buttonProps;
  const isPressed = toggleGroupControlContext.value === value;
  const cx = (0, _hooks.useCx)();
  const labelViewClasses = (0, _element.useMemo)(() => cx(isBlock && styles.labelBlock), [cx, isBlock]);
  const itemClasses = (0, _element.useMemo)(() => cx(styles.buttonView({
    isDeselectable,
    isIcon,
    isPressed,
    size
  }), className), [cx, isDeselectable, isIcon, isPressed, size, className]);
  const backdropClasses = (0, _element.useMemo)(() => cx(styles.backdropView), [cx]);
  const buttonOnClick = () => {
    if (isDeselectable && isPressed) {
      toggleGroupControlContext.setValue(undefined);
    } else {
      toggleGroupControlContext.setValue(value);
    }
  };
  const commonProps = {
    ...otherButtonProps,
    className: itemClasses,
    'data-value': value,
    ref: forwardedRef
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(LabelView, {
    className: labelViewClasses,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(WithToolTip, {
      showTooltip: showTooltip,
      text: otherButtonProps['aria-label'],
      children: isDeselectable ? /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
        ...commonProps,
        onFocus: onFocusProp,
        "aria-pressed": isPressed,
        type: "button",
        onClick: buttonOnClick,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(ButtonContentView, {
          children: children
        })
      }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(Ariakit.Radio, {
        render: /*#__PURE__*/(0, _jsxRuntime.jsx)("button", {
          type: "button",
          ...commonProps,
          onFocus: event => {
            onFocusProp?.(event);
            if (event.defaultPrevented) {
              return;
            }
            toggleGroupControlContext.setValue(value);
          }
        }),
        value: value,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(ButtonContentView, {
          children: children
        })
      })
    }), isPressed ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_framerMotion.motion.div, {
      layout: true,
      layoutRoot: true,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_framerMotion.motion.div, {
        className: backdropClasses,
        transition: shouldReduceMotion ? REDUCED_MOTION_TRANSITION_CONFIG : undefined,
        role: "presentation",
        layoutId: LAYOUT_ID
      })
    }) : null]
  });
}

/**
 * `ToggleGroupControlOptionBase` is a form component and is meant to be used as an internal,
 * generic component for any children of `ToggleGroupControl`.
 *
 * @example
 * ```jsx
 * import {
 *   __experimentalToggleGroupControl as ToggleGroupControl,
 *   __experimentalToggleGroupControlOptionBase as ToggleGroupControlOptionBase,
 * } from '@wordpress/components';
 *
 * function Example() {
 *   return (
 *     <ToggleGroupControl label="my label" value="vertical" isBlock>
 *       <ToggleGroupControlOption value="horizontal" label="Horizontal" />
 *       <ToggleGroupControlOption value="vertical" label="Vertical" />
 *     </ToggleGroupControl>
 *   );
 * }
 * ```
 */
const ConnectedToggleGroupControlOptionBase = (0, _context.contextConnect)(ToggleGroupControlOptionBase, 'ToggleGroupControlOptionBase');
var _default = exports.default = ConnectedToggleGroupControlOptionBase;
//# sourceMappingURL=component.js.map