"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _blockEditor = require("@wordpress/block-editor");
var _i18n = require("@wordpress/i18n");
var _components = require("@wordpress/components");
var _icons = require("@wordpress/icons");
var _element = require("@wordpress/element");
var _lockUnlock = require("../../../lock-unlock");
var _subtitle = _interopRequireDefault(require("../subtitle"));
var _navigationButton = require("../navigation-button");
var _utils = require("../utils");
var _header = _interopRequireDefault(require("../header"));
var _confirmResetFontSizesDialog = _interopRequireDefault(require("./confirm-reset-font-sizes-dialog"));
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  Menu
} = (0, _lockUnlock.unlock)(_components.privateApis);
const {
  useGlobalSetting
} = (0, _lockUnlock.unlock)(_blockEditor.privateApis);
function FontSizeGroup({
  label,
  origin,
  sizes,
  handleAddFontSize,
  handleResetFontSizes
}) {
  const [isResetDialogOpen, setIsResetDialogOpen] = (0, _element.useState)(false);
  const toggleResetDialog = () => setIsResetDialogOpen(!isResetDialogOpen);
  const resetDialogText = origin === 'custom' ? (0, _i18n.__)('Are you sure you want to remove all custom font size presets?') : (0, _i18n.__)('Are you sure you want to reset all font size presets to their default values?');
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [isResetDialogOpen && /*#__PURE__*/(0, _jsxRuntime.jsx)(_confirmResetFontSizesDialog.default, {
      text: resetDialogText,
      confirmButtonText: origin === 'custom' ? (0, _i18n.__)('Remove') : (0, _i18n.__)('Reset'),
      isOpen: isResetDialogOpen,
      toggleOpen: toggleResetDialog,
      onConfirm: handleResetFontSizes
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalVStack, {
      spacing: 4,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalHStack, {
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_subtitle.default, {
          level: 3,
          children: label
        }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.FlexItem, {
          className: "edit-site-global-styles__typography-panel__options-container",
          children: [origin === 'custom' && /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
            label: (0, _i18n.__)('Add font size'),
            icon: _icons.plus,
            size: "small",
            onClick: handleAddFontSize
          }), !!handleResetFontSizes && /*#__PURE__*/(0, _jsxRuntime.jsxs)(Menu, {
            children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(Menu.TriggerButton, {
              render: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
                size: "small",
                icon: _icons.moreVertical,
                label: (0, _i18n.__)('Font size presets options')
              })
            }), /*#__PURE__*/(0, _jsxRuntime.jsx)(Menu.Popover, {
              children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Menu.Item, {
                onClick: toggleResetDialog,
                children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Menu.ItemLabel, {
                  children: origin === 'custom' ? (0, _i18n.__)('Remove font size presets') : (0, _i18n.__)('Reset font size presets')
                })
              })
            })]
          })]
        })]
      }), !!sizes.length && /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalItemGroup, {
        isBordered: true,
        isSeparated: true,
        children: sizes.map(size => /*#__PURE__*/(0, _jsxRuntime.jsx)(_navigationButton.NavigationButtonAsItem, {
          path: `/typography/font-sizes/${origin}/${size.slug}`,
          children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalHStack, {
            children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.FlexItem, {
              className: "edit-site-font-size__item",
              children: size.name
            }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.FlexItem, {
              display: "flex",
              children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_icons.Icon, {
                icon: (0, _i18n.isRTL)() ? _icons.chevronLeft : _icons.chevronRight
              })
            })]
          })
        }, size.slug))
      })]
    })]
  });
}
function FontSizes() {
  const [themeFontSizes, setThemeFontSizes] = useGlobalSetting('typography.fontSizes.theme');
  const [baseThemeFontSizes] = useGlobalSetting('typography.fontSizes.theme', null, 'base');
  const [defaultFontSizes, setDefaultFontSizes] = useGlobalSetting('typography.fontSizes.default');
  const [baseDefaultFontSizes] = useGlobalSetting('typography.fontSizes.default', null, 'base');
  const [customFontSizes = [], setCustomFontSizes] = useGlobalSetting('typography.fontSizes.custom');
  const [defaultFontSizesEnabled] = useGlobalSetting('typography.defaultFontSizes');
  const handleAddFontSize = () => {
    const index = (0, _utils.getNewIndexFromPresets)(customFontSizes, 'custom-');
    const newFontSize = {
      /* translators: %d: font size index */
      name: (0, _i18n.sprintf)((0, _i18n.__)('New Font Size %d'), index),
      size: '16px',
      slug: `custom-${index}`
    };
    setCustomFontSizes([...customFontSizes, newFontSize]);
  };
  const hasSameSizeValues = (arr1, arr2) => arr1.map(item => item.size).join('') === arr2.map(item => item.size).join('');
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalVStack, {
    spacing: 2,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_header.default, {
      title: (0, _i18n.__)('Font size presets'),
      description: (0, _i18n.__)('Create and edit the presets used for font sizes across the site.')
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalView, {
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.__experimentalSpacer, {
        paddingX: 4,
        children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.__experimentalVStack, {
          spacing: 8,
          children: [!!themeFontSizes?.length && /*#__PURE__*/(0, _jsxRuntime.jsx)(FontSizeGroup, {
            label: (0, _i18n.__)('Theme'),
            origin: "theme",
            sizes: themeFontSizes,
            baseSizes: baseThemeFontSizes,
            handleAddFontSize: handleAddFontSize,
            handleResetFontSizes: hasSameSizeValues(themeFontSizes, baseThemeFontSizes) ? null : () => setThemeFontSizes(baseThemeFontSizes)
          }), defaultFontSizesEnabled && !!defaultFontSizes?.length && /*#__PURE__*/(0, _jsxRuntime.jsx)(FontSizeGroup, {
            label: (0, _i18n.__)('Default'),
            origin: "default",
            sizes: defaultFontSizes,
            baseSizes: baseDefaultFontSizes,
            handleAddFontSize: handleAddFontSize,
            handleResetFontSizes: hasSameSizeValues(defaultFontSizes, baseDefaultFontSizes) ? null : () => setDefaultFontSizes(baseDefaultFontSizes)
          }), /*#__PURE__*/(0, _jsxRuntime.jsx)(FontSizeGroup, {
            label: (0, _i18n.__)('Custom'),
            origin: "custom",
            sizes: customFontSizes,
            handleAddFontSize: handleAddFontSize,
            handleResetFontSizes: customFontSizes.length > 0 ? () => setCustomFontSizes([]) : null
          })]
        })
      })
    })]
  });
}
var _default = exports.default = FontSizes;
//# sourceMappingURL=font-sizes.js.map