"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _clsx = _interopRequireDefault(require("clsx"));
var _components = require("@wordpress/components");
var _blocks = require("@wordpress/blocks");
var _element = require("@wordpress/element");
var _blockContext = _interopRequireDefault(require("../block-context"));
var _jsxRuntime = require("react/jsx-runtime");
/**
 * External dependencies
 */

/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

/**
 * Default value used for blocks which do not define their own context needs,
 * used to guarantee that a block's `context` prop will always be an object. It
 * is assigned as a constant since it is always expected to be an empty object,
 * and in order to avoid unnecessary React reconciliations of a changing object.
 *
 * @type {{}}
 */const DEFAULT_BLOCK_CONTEXT = {};
const Edit = props => {
  const {
    name
  } = props;
  const blockType = (0, _blocks.getBlockType)(name);
  if (!blockType) {
    return null;
  }

  // `edit` and `save` are functions or components describing the markup
  // with which a block is displayed. If `blockType` is valid, assign
  // them preferentially as the render value for the block.
  const Component = blockType.edit || blockType.save;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
    ...props
  });
};
const EditWithFilters = (0, _components.withFilters)('editor.BlockEdit')(Edit);
const EditWithGeneratedProps = props => {
  const {
    attributes = {},
    name
  } = props;
  const blockType = (0, _blocks.getBlockType)(name);
  const blockContext = (0, _element.useContext)(_blockContext.default);

  // Assign context values using the block type's declared context needs.
  const context = (0, _element.useMemo)(() => {
    return blockType && blockType.usesContext ? Object.fromEntries(Object.entries(blockContext).filter(([key]) => blockType.usesContext.includes(key))) : DEFAULT_BLOCK_CONTEXT;
  }, [blockType, blockContext]);
  if (!blockType) {
    return null;
  }
  if (blockType.apiVersion > 1) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(EditWithFilters, {
      ...props,
      context: context
    });
  }

  // Generate a class name for the block's editable form.
  const generatedClassName = (0, _blocks.hasBlockSupport)(blockType, 'className', true) ? (0, _blocks.getBlockDefaultClassName)(name) : null;
  const className = (0, _clsx.default)(generatedClassName, attributes.className, props.className);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(EditWithFilters, {
    ...props,
    context: context,
    className: className
  });
};
var _default = exports.default = EditWithGeneratedProps;
//# sourceMappingURL=edit.js.map