/**
 * WordPress dependencies
 */
import { __ } from '@wordpress/i18n';
import { __experimentalText as Text, __experimentalItemGroup as ItemGroup, __experimentalVStack as VStack, __experimentalHStack as HStack, Button } from '@wordpress/components';
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
import { settings } from '@wordpress/icons';
import { useContext } from '@wordpress/element';

/**
 * Internal dependencies
 */
import FontLibraryProvider, { FontLibraryContext } from './font-library-modal/context';
import FontLibraryModal from './font-library-modal';
import FontFamilyItem from './font-family-item';
import Subtitle from './subtitle';
import { setUIValuesNeeded } from './font-library-modal/utils';
import { unlock } from '../../lock-unlock';
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
const {
  useGlobalSetting
} = unlock(blockEditorPrivateApis);

/**
 * 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 => setUIValuesNeeded(f, {
    source
  })) : [];
}
function FontFamilies() {
  const {
    baseCustomFonts,
    modalTabOpen,
    setModalTabOpen
  } = useContext(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__*/_jsxs(_Fragment, {
    children: [!!modalTabOpen && /*#__PURE__*/_jsx(FontLibraryModal, {
      onRequestClose: () => setModalTabOpen(null),
      defaultTabId: modalTabOpen
    }), /*#__PURE__*/_jsxs(VStack, {
      spacing: 2,
      children: [/*#__PURE__*/_jsxs(HStack, {
        justify: "space-between",
        children: [/*#__PURE__*/_jsx(Subtitle, {
          level: 3,
          children: __('Fonts')
        }), /*#__PURE__*/_jsx(Button, {
          onClick: () => setModalTabOpen('installed-fonts'),
          label: __('Manage fonts'),
          icon: settings,
          size: "small"
        })]
      }), activeFonts.length > 0 && /*#__PURE__*/_jsx(_Fragment, {
        children: /*#__PURE__*/_jsx(ItemGroup, {
          size: "large",
          isBordered: true,
          isSeparated: true,
          children: activeFonts.map(font => /*#__PURE__*/_jsx(FontFamilyItem, {
            font: font
          }, font.slug))
        })
      }), !hasFonts && /*#__PURE__*/_jsxs(_Fragment, {
        children: [/*#__PURE__*/_jsx(Text, {
          as: "p",
          children: hasInstalledFonts ? __('No fonts activated.') : __('No fonts installed.')
        }), /*#__PURE__*/_jsx(Button, {
          className: "edit-site-global-styles-font-families__manage-fonts",
          variant: "secondary",
          __next40pxDefaultSize: true,
          onClick: () => {
            setModalTabOpen(hasInstalledFonts ? 'installed-fonts' : 'upload-fonts');
          },
          children: hasInstalledFonts ? __('Manage fonts') : __('Add fonts')
        })]
      })]
    })]
  });
}
export default ({
  ...props
}) => /*#__PURE__*/_jsx(FontLibraryProvider, {
  children: /*#__PURE__*/_jsx(FontFamilies, {
    ...props
  })
});
//# sourceMappingURL=font-families.js.map