"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getActiveStyle = getActiveStyle;
exports.getDefaultStyle = getDefaultStyle;
exports.getRenderedStyles = getRenderedStyles;
exports.replaceActiveStyle = replaceActiveStyle;
var _tokenList = _interopRequireDefault(require("@wordpress/token-list"));
var _i18n = require("@wordpress/i18n");
/**
 * WordPress dependencies
 */

/**
 * Returns the active style from the given className.
 *
 * @param {Array}  styles    Block styles.
 * @param {string} className Class name
 *
 * @return {Object?} The active style.
 */
function getActiveStyle(styles, className) {
  for (const style of new _tokenList.default(className).values()) {
    if (style.indexOf('is-style-') === -1) {
      continue;
    }
    const potentialStyleName = style.substring(9);
    const activeStyle = styles?.find(({
      name
    }) => name === potentialStyleName);
    if (activeStyle) {
      return activeStyle;
    }
  }
  return getDefaultStyle(styles);
}

/**
 * Replaces the active style in the block's className.
 *
 * @param {string}  className   Class name.
 * @param {Object?} activeStyle The replaced style.
 * @param {Object}  newStyle    The replacing style.
 *
 * @return {string} The updated className.
 */
function replaceActiveStyle(className, activeStyle, newStyle) {
  const list = new _tokenList.default(className);
  if (activeStyle) {
    list.remove('is-style-' + activeStyle.name);
  }
  list.add('is-style-' + newStyle.name);
  return list.value;
}

/**
 * Returns a collection of styles that can be represented on the frontend.
 * The function checks a style collection for a default style. If none is found, it adds one to
 * act as a fallback for when there is no active style applied to a block. The default item also serves
 * as a switch on the frontend to deactivate non-default styles.
 *
 * @param {Array} styles Block styles.
 *
 * @return {Array<Object?>}        The style collection.
 */
function getRenderedStyles(styles) {
  if (!styles || styles.length === 0) {
    return [];
  }
  return getDefaultStyle(styles) ? styles : [{
    name: 'default',
    label: (0, _i18n._x)('Default', 'block style'),
    isDefault: true
  }, ...styles];
}

/**
 * Returns a style object from a collection of styles where that style object is the default block style.
 *
 * @param {Array} styles Block styles.
 *
 * @return {Object?}        The default style object, if found.
 */
function getDefaultStyle(styles) {
  return styles?.find(style => style.isDefault);
}
//# sourceMappingURL=utils.js.map