"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-library/src/query/edit/inspector-controls/author-control.js
var author_control_exports = {};
__export(author_control_exports, {
  default: () => author_control_default
});
module.exports = __toCommonJS(author_control_exports);
var import_i18n = require("@wordpress/i18n");
var import_components = require("@wordpress/components");
var import_data = require("@wordpress/data");
var import_core_data = require("@wordpress/core-data");
var import_utils = require("../../utils.cjs");
var import_jsx_runtime = require("react/jsx-runtime");
var AUTHORS_QUERY = {
  who: "authors",
  per_page: -1,
  _fields: "id,name",
  context: "view"
};
function AuthorControl({ value, onChange }) {
  const authorsList = (0, import_data.useSelect)((select) => {
    const { getUsers } = select(import_core_data.store);
    return getUsers(AUTHORS_QUERY);
  }, []);
  if (!authorsList) {
    return null;
  }
  const authorsInfo = (0, import_utils.getEntitiesInfo)(authorsList);
  const normalizedValue = !value ? [] : value.toString().split(",");
  const sanitizedValue = normalizedValue.reduce(
    (accumulator, authorId) => {
      const author = authorsInfo.mapById[authorId];
      if (author) {
        accumulator.push({
          id: authorId,
          value: author.name
        });
      }
      return accumulator;
    },
    []
  );
  const getIdByValue = (entitiesMappedByName, authorValue) => {
    const id = authorValue?.id || entitiesMappedByName[authorValue]?.id;
    if (id) {
      return id;
    }
  };
  const onAuthorChange = (newValue) => {
    const ids = Array.from(
      newValue.reduce((accumulator, author) => {
        const id = getIdByValue(authorsInfo.mapByName, author);
        if (id) {
          accumulator.add(id);
        }
        return accumulator;
      }, /* @__PURE__ */ new Set())
    );
    onChange({ author: ids.join(",") });
  };
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_components.FormTokenField,
    {
      label: (0, import_i18n.__)("Authors"),
      value: sanitizedValue,
      suggestions: authorsInfo.names,
      onChange: onAuthorChange,
      help: "",
      __next40pxDefaultSize: true
    }
  );
}
var author_control_default = AuthorControl;
//# sourceMappingURL=author-control.cjs.map
