"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _components = require("@wordpress/components");
var _data = require("@wordpress/data");
var _element = require("@wordpress/element");
var _i18n = require("@wordpress/i18n");
var _groups = _interopRequireDefault(require("../inspector-controls/groups"));
var _inspectorControls = _interopRequireDefault(require("../inspector-controls"));
var _store = require("../../store");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const PositionControlsPanel = () => {
  const [initialOpen, setInitialOpen] = (0, _element.useState)();

  // Determine whether the panel should be expanded.
  const {
    multiSelectedBlocks
  } = (0, _data.useSelect)(select => {
    const {
      getBlocksByClientId,
      getSelectedBlockClientIds
    } = select(_store.store);
    const clientIds = getSelectedBlockClientIds();
    return {
      multiSelectedBlocks: getBlocksByClientId(clientIds)
    };
  }, []);
  (0, _element.useLayoutEffect)(() => {
    // If any selected block has a position set, open the panel by default.
    // The first block's value will still be used within the control though.
    if (initialOpen === undefined) {
      setInitialOpen(multiSelectedBlocks.some(({
        attributes
      }) => !!attributes?.style?.position?.type));
    }
  }, [initialOpen, multiSelectedBlocks, setInitialOpen]);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.PanelBody, {
    className: "block-editor-block-inspector__position",
    title: (0, _i18n.__)('Position'),
    initialOpen: initialOpen !== null && initialOpen !== void 0 ? initialOpen : false,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_inspectorControls.default.Slot, {
      group: "position"
    })
  });
};
const PositionControls = () => {
  const fills = (0, _components.__experimentalUseSlotFills)(_groups.default.position.name);
  const hasFills = Boolean(fills && fills.length);
  if (!hasFills) {
    return null;
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(PositionControlsPanel, {});
};
var _default = exports.default = PositionControls;
//# sourceMappingURL=position-controls-panel.js.map