"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _i18n = require("@wordpress/i18n");
var _components = require("@wordpress/components");
var _blockEditor = require("@wordpress/block-editor");
var _icons = require("@wordpress/icons");
var _element = require("@wordpress/element");
var _context = _interopRequireWildcard(require("./font-library-modal/context"));
var _fontLibraryModal = _interopRequireDefault(require("./font-library-modal"));
var _fontFamilyItem = _interopRequireDefault(require("./font-family-item"));
var _subtitle = _interopRequireDefault(require("./subtitle"));
var _utils = require("./font-library-modal/utils");
var _lockUnlock = require("../../lock-unlock");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  useGlobalSetting
} = (0, _lockUnlock.unlock)(_blockEditor.privateApis);

/**
 * Maps the fonts with the source, if available.
 *
 * @param {Array}  fonts  The fonts to map.
 * @param {string} source The source of the fonts.
 * @return {Array} The mapped fonts.
 */
function mapFontsWithSource(fonts, source) {
  return fonts ? fonts.map(f => (0, _utils.setUIValuesNeeded)(f, {
    source
  })) : [];
}
function FontFamilies() {
  const {
    baseCustomFonts,
    modalTabOpen,
    setModalTabOpen
  } = (0, _element.useContext)(_context.FontLibraryContext);
  const [fontFamilies] = useGlobalSetting('typography.fontFamilies');
  const [baseFontFamilies] = useGlobalSetting('typography.fontFamilies', undefined, 'base');
  const themeFonts = mapFontsWithSource(fontFamilies?.theme, 'theme');
  const customFonts = mapFontsWithSource(fontFamilies?.custom, 'custom');
  const activeFonts = [...themeFonts, ...customFonts].sort((a, b) => a.name.localeCompare(b.name));
  const hasFonts = 0 < activeFonts.length;
  const hasInstalledFonts = hasFonts || baseFontFamilies?.theme?.length > 0 || baseCustomFonts?.length > 0;
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [!!modalTabOpen && /*#__PURE__*/(0, _jsxRuntime.jsx)(_fontLibraryModal.default, {
      onRequestClose: () => setModalTabOpen(null),
      defaultTabId: modalTabOpen
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalVStack, {
      spacing: 2,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalHStack, {
        justify: "space-between",
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_subtitle.default, {
          level: 3,
          children: (0, _i18n.__)('Fonts')
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
          onClick: () => setModalTabOpen('installed-fonts'),
          label: (0, _i18n.__)('Manage fonts'),
          icon: _icons.settings,
          size: "small"
        })]
      }), activeFonts.length > 0 && /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalItemGroup, {
          size: "large",
          isBordered: true,
          isSeparated: true,
          children: activeFonts.map(font => /*#__PURE__*/(0, _jsxRuntime.jsx)(_fontFamilyItem.default, {
            font: font
          }, font.slug))
        })
      }), !hasFonts && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalText, {
          as: "p",
          children: hasInstalledFonts ? (0, _i18n.__)('No fonts activated.') : (0, _i18n.__)('No fonts installed.')
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
          className: "edit-site-global-styles-font-families__manage-fonts",
          variant: "secondary",
          __next40pxDefaultSize: true,
          onClick: () => {
            setModalTabOpen(hasInstalledFonts ? 'installed-fonts' : 'upload-fonts');
          },
          children: hasInstalledFonts ? (0, _i18n.__)('Manage fonts') : (0, _i18n.__)('Add fonts')
        })]
      })]
    })]
  });
}
var _default = ({
  ...props
}) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_context.default, {
  children: /*#__PURE__*/(0, _jsxRuntime.jsx)(FontFamilies, {
    ...props
  })
});
exports.default = _default;
//# sourceMappingURL=font-families.js.map