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

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _element = require("@wordpress/element");
var _coreData = require("@wordpress/core-data");
var _data = require("@wordpress/data");
var _lockUnlock = require("../../lock-unlock");
var _store = require("../../store");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

function usePostFields({
  postType
}) {
  const {
    registerPostTypeSchema
  } = (0, _lockUnlock.unlock)((0, _data.useDispatch)(_store.store));
  (0, _element.useEffect)(() => {
    registerPostTypeSchema(postType);
  }, [registerPostTypeSchema, postType]);
  const {
    defaultFields
  } = (0, _data.useSelect)(select => {
    const {
      getEntityFields
    } = (0, _lockUnlock.unlock)(select(_store.store));
    return {
      defaultFields: getEntityFields('postType', postType)
    };
  }, [postType]);
  const {
    records: authors,
    isResolving: isLoadingAuthors
  } = (0, _coreData.useEntityRecords)('root', 'user', {
    per_page: -1
  });
  const fields = (0, _element.useMemo)(() => defaultFields.map(field => {
    if (field.id === 'author') {
      return {
        ...field,
        elements: authors?.map(({
          id,
          name
        }) => ({
          value: id,
          label: name
        }))
      };
    }
    return field;
  }), [authors, defaultFields]);
  return {
    isLoading: isLoadingAuthors,
    fields
  };
}

/**
 * Hook to get the fields for a post (BasePost or BasePostWithEmbeddedAuthor).
 */
var _default = exports.default = usePostFields;
//# sourceMappingURL=index.js.map