"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _i18n = require("@wordpress/i18n");
var _element = require("@wordpress/element");
var _components = require("@wordpress/components");
var _icons = require("@wordpress/icons");
var _deprecated = _interopRequireDefault(require("@wordpress/deprecated"));
var _linkViewer = _interopRequireDefault(require("./link-viewer"));
var _linkEditor = _interopRequireDefault(require("./link-editor"));
var _lockUnlock = require("../../lock-unlock");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  __experimentalPopoverLegacyPositionToPlacement
} = (0, _lockUnlock.unlock)(_components.privateApis);
const DEFAULT_PLACEMENT = 'bottom';
const URLPopover = (0, _element.forwardRef)(({
  additionalControls,
  children,
  renderSettings,
  // The DEFAULT_PLACEMENT value is assigned inside the function's body
  placement,
  focusOnMount = 'firstElement',
  // Deprecated
  position,
  // Rest
  ...popoverProps
}, ref) => {
  if (position !== undefined) {
    (0, _deprecated.default)('`position` prop in wp.blockEditor.URLPopover', {
      since: '6.2',
      alternative: '`placement` prop'
    });
  }

  // Compute popover's placement:
  // - give priority to `placement` prop, if defined
  // - otherwise, compute it from the legacy `position` prop (if defined)
  // - finally, fallback to the DEFAULT_PLACEMENT.
  let computedPlacement;
  if (placement !== undefined) {
    computedPlacement = placement;
  } else if (position !== undefined) {
    computedPlacement = __experimentalPopoverLegacyPositionToPlacement(position);
  }
  computedPlacement = computedPlacement || DEFAULT_PLACEMENT;
  const [isSettingsExpanded, setIsSettingsExpanded] = (0, _element.useState)(false);
  const showSettings = !!renderSettings && isSettingsExpanded;
  const toggleSettingsVisibility = () => {
    setIsSettingsExpanded(!isSettingsExpanded);
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.Popover, {
    ref: ref,
    role: "dialog",
    "aria-modal": "true",
    "aria-label": (0, _i18n.__)('Edit URL'),
    className: "block-editor-url-popover",
    focusOnMount: focusOnMount,
    placement: computedPlacement,
    shift: true,
    variant: "toolbar",
    ...popoverProps,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "block-editor-url-popover__input-container",
      children: /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: "block-editor-url-popover__row",
        children: [children, !!renderSettings && /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
          className: "block-editor-url-popover__settings-toggle",
          icon: _icons.chevronDown,
          label: (0, _i18n.__)('Link settings'),
          onClick: toggleSettingsVisibility,
          "aria-expanded": isSettingsExpanded,
          size: "compact"
        })]
      })
    }), showSettings && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "block-editor-url-popover__settings",
      children: renderSettings()
    }), additionalControls && !showSettings && /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      className: "block-editor-url-popover__additional-controls",
      children: additionalControls
    })]
  });
});
URLPopover.LinkEditor = _linkEditor.default;
URLPopover.LinkViewer = _linkViewer.default;

/**
 * @see https://github.com/WordPress/gutenberg/blob/HEAD/packages/block-editor/src/components/url-popover/README.md
 */
var _default = exports.default = URLPopover;
//# sourceMappingURL=index.js.map