/* wp:polyfill */
"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 _coreData = require("@wordpress/core-data");
var _data = require("@wordpress/data");
var _element = require("@wordpress/element");
var _installedFonts = _interopRequireDefault(require("./installed-fonts"));
var _fontCollection = _interopRequireDefault(require("./font-collection"));
var _uploadFonts = _interopRequireDefault(require("./upload-fonts"));
var _context = require("./context");
var _lockUnlock = require("../../../lock-unlock");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  Tabs
} = (0, _lockUnlock.unlock)(_components.privateApis);
const DEFAULT_TAB = {
  id: 'installed-fonts',
  title: (0, _i18n._x)('Library', 'Font library')
};
const UPLOAD_TAB = {
  id: 'upload-fonts',
  title: (0, _i18n._x)('Upload', 'noun')
};
const tabsFromCollections = collections => collections.map(({
  slug,
  name
}) => ({
  id: slug,
  title: collections.length === 1 && slug === 'google-fonts' ? (0, _i18n.__)('Install Fonts') : name
}));
function FontLibraryModal({
  onRequestClose,
  defaultTabId = 'installed-fonts'
}) {
  const {
    collections
  } = (0, _element.useContext)(_context.FontLibraryContext);
  const canUserCreate = (0, _data.useSelect)(select => {
    return select(_coreData.store).canUser('create', {
      kind: 'postType',
      name: 'wp_font_family'
    });
  }, []);
  const tabs = [DEFAULT_TAB];
  if (canUserCreate) {
    tabs.push(UPLOAD_TAB);
    tabs.push(...tabsFromCollections(collections || []));
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Modal, {
    title: (0, _i18n.__)('Fonts'),
    onRequestClose: onRequestClose,
    isFullScreen: true,
    className: "font-library-modal",
    children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(Tabs, {
      defaultTabId: defaultTabId,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: "font-library-modal__tablist-container",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Tabs.TabList, {
          children: tabs.map(({
            id,
            title
          }) => /*#__PURE__*/(0, _jsxRuntime.jsx)(Tabs.Tab, {
            tabId: id,
            children: title
          }, id))
        })
      }), tabs.map(({
        id
      }) => {
        let contents;
        switch (id) {
          case 'upload-fonts':
            contents = /*#__PURE__*/(0, _jsxRuntime.jsx)(_uploadFonts.default, {});
            break;
          case 'installed-fonts':
            contents = /*#__PURE__*/(0, _jsxRuntime.jsx)(_installedFonts.default, {});
            break;
          default:
            contents = /*#__PURE__*/(0, _jsxRuntime.jsx)(_fontCollection.default, {
              slug: id
            });
        }
        return /*#__PURE__*/(0, _jsxRuntime.jsx)(Tabs.TabPanel, {
          tabId: id,
          focusable: false,
          children: contents
        }, id);
      })]
    })
  });
}
var _default = exports.default = FontLibraryModal;
//# sourceMappingURL=index.js.map