/* wp:polyfill */
"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = EntityTypeList;
var _i18n = require("@wordpress/i18n");
var _data = require("@wordpress/data");
var _components = require("@wordpress/components");
var _coreData = require("@wordpress/core-data");
var _blockEditor = require("@wordpress/block-editor");
var _element = require("@wordpress/element");
var _entityRecordItem = _interopRequireDefault(require("./entity-record-item"));
var _lockUnlock = require("../../lock-unlock");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  getGlobalStylesChanges,
  GlobalStylesContext
} = (0, _lockUnlock.unlock)(_blockEditor.privateApis);
function getEntityDescription(entity, count) {
  switch (entity) {
    case 'site':
      return 1 === count ? (0, _i18n.__)('This change will affect your whole site.') : (0, _i18n.__)('These changes will affect your whole site.');
    case 'wp_template':
      return (0, _i18n.__)('This change will affect pages and posts that use this template.');
    case 'page':
    case 'post':
      return (0, _i18n.__)('The following has been modified.');
  }
}
function GlobalStylesDescription({
  record
}) {
  const {
    user: currentEditorGlobalStyles
  } = (0, _element.useContext)(GlobalStylesContext);
  const savedRecord = (0, _data.useSelect)(select => select(_coreData.store).getEntityRecord(record.kind, record.name, record.key), [record.kind, record.name, record.key]);
  const globalStylesChanges = getGlobalStylesChanges(currentEditorGlobalStyles, savedRecord, {
    maxResults: 10
  });
  return globalStylesChanges.length ? /*#__PURE__*/(0, _jsxRuntime.jsx)("ul", {
    className: "entities-saved-states__changes",
    children: globalStylesChanges.map(change => /*#__PURE__*/(0, _jsxRuntime.jsx)("li", {
      children: change
    }, change))
  }) : null;
}
function EntityDescription({
  record,
  count
}) {
  if ('globalStyles' === record?.name) {
    return null;
  }
  const description = getEntityDescription(record?.name, count);
  return description ? /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.PanelRow, {
    children: description
  }) : null;
}
function EntityTypeList({
  list,
  unselectedEntities,
  setUnselectedEntities
}) {
  const count = list.length;
  const firstRecord = list[0];
  const entityConfig = (0, _data.useSelect)(select => select(_coreData.store).getEntityConfig(firstRecord.kind, firstRecord.name), [firstRecord.kind, firstRecord.name]);
  let entityLabel = entityConfig.label;
  if (firstRecord?.name === 'wp_template_part') {
    entityLabel = 1 === count ? (0, _i18n.__)('Template Part') : (0, _i18n.__)('Template Parts');
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.PanelBody, {
    title: entityLabel,
    initialOpen: true,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(EntityDescription, {
      record: firstRecord,
      count: count
    }), list.map(record => {
      return /*#__PURE__*/(0, _jsxRuntime.jsx)(_entityRecordItem.default, {
        record: record,
        checked: !unselectedEntities.some(elt => elt.kind === record.kind && elt.name === record.name && elt.key === record.key && elt.property === record.property),
        onChange: value => setUnselectedEntities(record, value)
      }, record.key || record.property);
    }), 'globalStyles' === firstRecord?.name && /*#__PURE__*/(0, _jsxRuntime.jsx)(GlobalStylesDescription, {
      record: firstRecord
    })]
  });
}
//# sourceMappingURL=entity-type-list.js.map