"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = FormatEdit;
exports.usesContextKey = void 0;
var _richText = require("@wordpress/rich-text");
var _element = require("@wordpress/element");
var _blockContext = _interopRequireDefault(require("../block-context"));
var _jsxRuntime = require("react/jsx-runtime");
var _react = require("react");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const DEFAULT_BLOCK_CONTEXT = {};
const usesContextKey = exports.usesContextKey = Symbol('usesContext');
function Edit({
  onChange,
  onFocus,
  value,
  forwardedRef,
  settings
}) {
  const {
    name,
    edit: EditFunction,
    [usesContextKey]: usesContext
  } = settings;
  const blockContext = (0, _element.useContext)(_blockContext.default);

  // Assign context values using the block type's declared context needs.
  const context = (0, _element.useMemo)(() => {
    return usesContext ? Object.fromEntries(Object.entries(blockContext).filter(([key]) => usesContext.includes(key))) : DEFAULT_BLOCK_CONTEXT;
  }, [usesContext, blockContext]);
  if (!EditFunction) {
    return null;
  }
  const activeFormat = (0, _richText.getActiveFormat)(value, name);
  const isActive = activeFormat !== undefined;
  const activeObject = (0, _richText.getActiveObject)(value);
  const isObjectActive = activeObject !== undefined && activeObject.type === name;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(EditFunction, {
    isActive: isActive,
    activeAttributes: isActive ? activeFormat.attributes || {} : {},
    isObjectActive: isObjectActive,
    activeObjectAttributes: isObjectActive ? activeObject.attributes || {} : {},
    value: value,
    onChange: onChange,
    onFocus: onFocus,
    contentRef: forwardedRef,
    context: context
  }, name);
}
function FormatEdit({
  formatTypes,
  ...props
}) {
  return formatTypes.map(settings => /*#__PURE__*/(0, _react.createElement)(Edit, {
    settings: settings,
    ...props,
    key: settings.name
  }));
}
//# sourceMappingURL=format-edit.js.map