/* wp:polyfill */
"use strict";

var _hooks = require("@wordpress/hooks");
var _patterns = require("@wordpress/patterns");
var _compose = require("@wordpress/compose");
var _blockEditor = require("@wordpress/block-editor");
var _data = require("@wordpress/data");
var _blocks = require("@wordpress/blocks");
var _store = require("../store");
var _lockUnlock = require("../lock-unlock");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

/** @typedef {import('@wordpress/blocks').WPBlockSettings} WPBlockSettings */const {
  PatternOverridesControls,
  ResetOverridesControl,
  PatternOverridesBlockControls,
  PATTERN_TYPES,
  PARTIAL_SYNCING_SUPPORTED_BLOCKS,
  PATTERN_SYNC_TYPES
} = (0, _lockUnlock.unlock)(_patterns.privateApis);

/**
 * Override the default edit UI to include a new block inspector control for
 * assigning a partial syncing controls to supported blocks in the pattern editor.
 * Currently, only the `core/paragraph` block is supported.
 *
 * @param {Component} BlockEdit Original component.
 *
 * @return {Component} Wrapped component.
 */
const withPatternOverrideControls = (0, _compose.createHigherOrderComponent)(BlockEdit => props => {
  const isSupportedBlock = !!PARTIAL_SYNCING_SUPPORTED_BLOCKS[props.name];
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(BlockEdit, {
      ...props
    }, "edit"), props.isSelected && isSupportedBlock && /*#__PURE__*/(0, _jsxRuntime.jsx)(ControlsWithStoreSubscription, {
      ...props
    }), isSupportedBlock && /*#__PURE__*/(0, _jsxRuntime.jsx)(PatternOverridesBlockControls, {})]
  });
}, 'withPatternOverrideControls');

// Split into a separate component to avoid a store subscription
// on every block.
function ControlsWithStoreSubscription(props) {
  const blockEditingMode = (0, _blockEditor.useBlockEditingMode)();
  const {
    hasPatternOverridesSource,
    isEditingSyncedPattern
  } = (0, _data.useSelect)(select => {
    const {
      getCurrentPostType,
      getEditedPostAttribute
    } = select(_store.store);
    return {
      // For editing link to the site editor if the theme and user permissions support it.
      hasPatternOverridesSource: !!(0, _blocks.getBlockBindingsSource)('core/pattern-overrides'),
      isEditingSyncedPattern: getCurrentPostType() === PATTERN_TYPES.user && getEditedPostAttribute('meta')?.wp_pattern_sync_status !== PATTERN_SYNC_TYPES.unsynced && getEditedPostAttribute('wp_pattern_sync_status') !== PATTERN_SYNC_TYPES.unsynced
    };
  }, []);
  const bindings = props.attributes.metadata?.bindings;
  const hasPatternBindings = !!bindings && Object.values(bindings).some(binding => binding.source === 'core/pattern-overrides');
  const shouldShowPatternOverridesControls = isEditingSyncedPattern && blockEditingMode === 'default';
  const shouldShowResetOverridesControl = !isEditingSyncedPattern && !!props.attributes.metadata?.name && blockEditingMode !== 'disabled' && hasPatternBindings;
  if (!hasPatternOverridesSource) {
    return null;
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [shouldShowPatternOverridesControls && /*#__PURE__*/(0, _jsxRuntime.jsx)(PatternOverridesControls, {
      ...props
    }), shouldShowResetOverridesControl && /*#__PURE__*/(0, _jsxRuntime.jsx)(ResetOverridesControl, {
      ...props
    })]
  });
}
(0, _hooks.addFilter)('editor.BlockEdit', 'core/editor/with-pattern-override-controls', withPatternOverrideControls);
//# sourceMappingURL=pattern-overrides.js.map