"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = InspectorControlsFill;
var _components = require("@wordpress/components");
var _warning = _interopRequireDefault(require("@wordpress/warning"));
var _deprecated = _interopRequireDefault(require("@wordpress/deprecated"));
var _element = require("@wordpress/element");
var _context = require("../block-edit/context");
var _groups = _interopRequireDefault(require("./groups"));
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

function InspectorControlsFill({
  children,
  group = 'default',
  __experimentalGroup,
  resetAllFilter
}) {
  if (__experimentalGroup) {
    (0, _deprecated.default)('`__experimentalGroup` property in `InspectorControlsFill`', {
      since: '6.2',
      version: '6.4',
      alternative: '`group`'
    });
    group = __experimentalGroup;
  }
  const context = (0, _context.useBlockEditContext)();
  const Fill = _groups.default[group]?.Fill;
  if (!Fill) {
    globalThis.SCRIPT_DEBUG === true ? (0, _warning.default)(`Unknown InspectorControls group "${group}" provided.`) : void 0;
    return null;
  }
  if (!context[_context.mayDisplayControlsKey]) {
    return null;
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalStyleProvider, {
    document: document,
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Fill, {
      children: fillProps => {
        return /*#__PURE__*/(0, _jsxRuntime.jsx)(ToolsPanelInspectorControl, {
          fillProps: fillProps,
          children: children,
          resetAllFilter: resetAllFilter
        });
      }
    })
  });
}
function RegisterResetAll({
  resetAllFilter,
  children
}) {
  const {
    registerResetAllFilter,
    deregisterResetAllFilter
  } = (0, _element.useContext)(_components.__experimentalToolsPanelContext);
  (0, _element.useEffect)(() => {
    if (resetAllFilter && registerResetAllFilter && deregisterResetAllFilter) {
      registerResetAllFilter(resetAllFilter);
      return () => {
        deregisterResetAllFilter(resetAllFilter);
      };
    }
  }, [resetAllFilter, registerResetAllFilter, deregisterResetAllFilter]);
  return children;
}
function ToolsPanelInspectorControl({
  children,
  resetAllFilter,
  fillProps
}) {
  // `fillProps.forwardedContext` is an array of context provider entries, provided by slot,
  // that should wrap the fill markup.
  const {
    forwardedContext = []
  } = fillProps;

  // Children passed to InspectorControlsFill will not have
  // access to any React Context whose Provider is part of
  // the InspectorControlsSlot tree. So we re-create the
  // Provider in this subtree.
  const innerMarkup = /*#__PURE__*/(0, _jsxRuntime.jsx)(RegisterResetAll, {
    resetAllFilter: resetAllFilter,
    children: children
  });
  return forwardedContext.reduce((inner, [Provider, props]) => /*#__PURE__*/(0, _jsxRuntime.jsx)(Provider, {
    ...props,
    children: inner
  }), innerMarkup);
}
//# sourceMappingURL=fill.js.map