"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.FONT_FAMILY_SUPPORT_KEY = void 0;
exports.resetFontFamily = resetFontFamily;
var _hooks = require("@wordpress/hooks");
var _blocks = require("@wordpress/blocks");
var _tokenList = _interopRequireDefault(require("@wordpress/token-list"));
var _components = require("@wordpress/components");
var _utils = require("./utils");
var _typography = require("./typography");
var _lockUnlock = require("../lock-unlock");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const FONT_FAMILY_SUPPORT_KEY = exports.FONT_FAMILY_SUPPORT_KEY = 'typography.__experimentalFontFamily';
const {
  kebabCase
} = (0, _lockUnlock.unlock)(_components.privateApis);

/**
 * Filters registered block settings, extending attributes to include
 * the `fontFamily` attribute.
 *
 * @param {Object} settings Original block settings
 * @return {Object}         Filtered block settings
 */
function addAttributes(settings) {
  if (!(0, _blocks.hasBlockSupport)(settings, FONT_FAMILY_SUPPORT_KEY)) {
    return settings;
  }

  // Allow blocks to specify a default value if needed.
  if (!settings.attributes.fontFamily) {
    Object.assign(settings.attributes, {
      fontFamily: {
        type: 'string'
      }
    });
  }
  return settings;
}

/**
 * Override props assigned to save component to inject font family.
 *
 * @param {Object} props      Additional props applied to save element
 * @param {Object} blockType  Block type
 * @param {Object} attributes Block attributes
 * @return {Object}           Filtered props applied to save element
 */
function addSaveProps(props, blockType, attributes) {
  if (!(0, _blocks.hasBlockSupport)(blockType, FONT_FAMILY_SUPPORT_KEY)) {
    return props;
  }
  if ((0, _utils.shouldSkipSerialization)(blockType, _typography.TYPOGRAPHY_SUPPORT_KEY, 'fontFamily')) {
    return props;
  }
  if (!attributes?.fontFamily) {
    return props;
  }

  // Use TokenList to dedupe classes.
  const classes = new _tokenList.default(props.className);
  classes.add(`has-${kebabCase(attributes?.fontFamily)}-font-family`);
  const newClassName = classes.value;
  props.className = newClassName ? newClassName : undefined;
  return props;
}
function useBlockProps({
  name,
  fontFamily
}) {
  return addSaveProps({}, name, {
    fontFamily
  });
}
var _default = exports.default = {
  useBlockProps,
  addSaveProps,
  attributeKeys: ['fontFamily'],
  hasSupport(name) {
    return (0, _blocks.hasBlockSupport)(name, FONT_FAMILY_SUPPORT_KEY);
  }
};
/**
 * Resets the font family block support attribute. This can be used when
 * disabling the font family support controls for a block via a progressive
 * discovery panel.
 *
 * @param {Object} props               Block props.
 * @param {Object} props.setAttributes Function to set block's attributes.
 */
function resetFontFamily({
  setAttributes
}) {
  setAttributes({
    fontFamily: undefined
  });
}
(0, _hooks.addFilter)('blocks.registerBlockType', 'core/fontFamily/addAttribute', addAttributes);
//# sourceMappingURL=font-family.js.map