"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = Shuffle;
var _icons = require("@wordpress/icons");
var _components = require("@wordpress/components");
var _i18n = require("@wordpress/i18n");
var _element = require("@wordpress/element");
var _data = require("@wordpress/data");
var _store = require("../../store");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const EMPTY_ARRAY = [];
function Container(props) {
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.ToolbarGroup, {
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.ToolbarButton, {
      ...props
    })
  });
}
function Shuffle({
  clientId,
  as = Container
}) {
  const {
    categories,
    patterns,
    patternName
  } = (0, _data.useSelect)(select => {
    const {
      getBlockAttributes,
      getBlockRootClientId,
      __experimentalGetAllowedPatterns
    } = select(_store.store);
    const attributes = getBlockAttributes(clientId);
    const _categories = attributes?.metadata?.categories || EMPTY_ARRAY;
    const _patternName = attributes?.metadata?.patternName;
    const rootBlock = getBlockRootClientId(clientId);
    const _patterns = __experimentalGetAllowedPatterns(rootBlock);
    return {
      categories: _categories,
      patterns: _patterns,
      patternName: _patternName
    };
  }, [clientId]);
  const {
    replaceBlocks
  } = (0, _data.useDispatch)(_store.store);
  const sameCategoryPatternsWithSingleWrapper = (0, _element.useMemo)(() => {
    if (!categories || categories.length === 0 || !patterns || patterns.length === 0) {
      return EMPTY_ARRAY;
    }
    return patterns.filter(pattern => {
      const isCorePattern = pattern.source === 'core' || pattern.source?.startsWith('pattern-directory') && pattern.source !== 'pattern-directory/theme';
      return (
        // Check if the pattern has only one top level block,
        // otherwise we may shuffle to pattern that will not allow to continue shuffling.
        pattern.blocks.length === 1 &&
        // We exclude the core patterns and pattern directory patterns that are not theme patterns.
        !isCorePattern && pattern.categories?.some(category => {
          return categories.includes(category);
        }) && (
        // Check if the pattern is not a synced pattern.
        pattern.syncStatus === 'unsynced' || !pattern.id)
      );
    });
  }, [categories, patterns]);
  if (sameCategoryPatternsWithSingleWrapper.length < 2) {
    return null;
  }
  function getNextPattern() {
    const numberOfPatterns = sameCategoryPatternsWithSingleWrapper.length;
    const patternIndex = sameCategoryPatternsWithSingleWrapper.findIndex(({
      name
    }) => name === patternName);
    const nextPatternIndex = patternIndex + 1 < numberOfPatterns ? patternIndex + 1 : 0;
    return sameCategoryPatternsWithSingleWrapper[nextPatternIndex];
  }
  const ComponentToUse = as;
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(ComponentToUse, {
    label: (0, _i18n.__)('Shuffle'),
    icon: _icons.shuffle,
    className: "block-editor-block-toolbar-shuffle",
    onClick: () => {
      const nextPattern = getNextPattern();
      nextPattern.blocks[0].attributes = {
        ...nextPattern.blocks[0].attributes,
        metadata: {
          ...nextPattern.blocks[0].attributes.metadata,
          categories
        }
      };
      replaceBlocks(clientId, nextPattern.blocks);
    }
  });
}
//# sourceMappingURL=shuffle.js.map