"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = useSpacingSizes;
var _element = require("@wordpress/element");
var _i18n = require("@wordpress/i18n");
var _useSettings = require("../../use-settings");
var _utils = require("../utils");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const EMPTY_ARRAY = [];
const compare = new Intl.Collator('und', {
  numeric: true
}).compare;
function useSpacingSizes() {
  const [customSpacingSizes, themeSpacingSizes, defaultSpacingSizes, defaultSpacingSizesEnabled] = (0, _useSettings.useSettings)('spacing.spacingSizes.custom', 'spacing.spacingSizes.theme', 'spacing.spacingSizes.default', 'spacing.defaultSpacingSizes');
  const customSizes = customSpacingSizes !== null && customSpacingSizes !== void 0 ? customSpacingSizes : EMPTY_ARRAY;
  const themeSizes = themeSpacingSizes !== null && themeSpacingSizes !== void 0 ? themeSpacingSizes : EMPTY_ARRAY;
  const defaultSizes = defaultSpacingSizes && defaultSpacingSizesEnabled !== false ? defaultSpacingSizes : EMPTY_ARRAY;
  return (0, _element.useMemo)(() => {
    const sizes = [{
      name: (0, _i18n.__)('None'),
      slug: '0',
      size: 0
    }, ...customSizes, ...themeSizes, ...defaultSizes];

    // Using numeric slugs opts-in to sorting by slug.
    if (sizes.every(({
      slug
    }) => /^[0-9]/.test(slug))) {
      sizes.sort((a, b) => compare(a.slug, b.slug));
    }
    return sizes.length > _utils.RANGE_CONTROL_MAX_SIZE ? [{
      name: (0, _i18n.__)('Default'),
      slug: 'default',
      size: undefined
    }, ...sizes] : sizes;
  }, [customSizes, themeSizes, defaultSizes]);
}
//# sourceMappingURL=use-spacing-sizes.js.map