"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.useAddedBy = useAddedBy;
var _coreData = require("@wordpress/core-data");
var _data = require("@wordpress/data");
var _icons = require("@wordpress/icons");
var _constants = require("../../utils/constants");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

/** @typedef {'wp_template'|'wp_template_part'} TemplateType */

/**
 * @typedef {'theme'|'plugin'|'site'|'user'} AddedByType
 *
 * @typedef AddedByData
 * @type {Object}
 * @property {AddedByType}  type         The type of the data.
 * @property {JSX.Element}  icon         The icon to display.
 * @property {string}       [imageUrl]   The optional image URL to display.
 * @property {string}       [text]       The text to display.
 * @property {boolean}      isCustomized Whether the template has been customized.
 *
 * @param    {TemplateType} postType     The template post type.
 * @param    {number}       postId       The template post id.
 * @return {AddedByData} The added by object or null.
 */
function useAddedBy(postType, postId) {
  return (0, _data.useSelect)(select => {
    const {
      getEntityRecord,
      getMedia,
      getUser,
      getEditedEntityRecord
    } = select(_coreData.store);
    const template = getEditedEntityRecord('postType', postType, postId);
    const originalSource = template?.original_source;
    const authorText = template?.author_text;
    switch (originalSource) {
      case 'theme':
        {
          return {
            type: originalSource,
            icon: _icons.layout,
            text: authorText,
            isCustomized: template.source === _constants.TEMPLATE_ORIGINS.custom
          };
        }
      case 'plugin':
        {
          return {
            type: originalSource,
            icon: _icons.plugins,
            text: authorText,
            isCustomized: template.source === _constants.TEMPLATE_ORIGINS.custom
          };
        }
      case 'site':
        {
          const siteData = getEntityRecord('root', '__unstableBase');
          return {
            type: originalSource,
            icon: _icons.globe,
            imageUrl: siteData?.site_logo ? getMedia(siteData.site_logo)?.source_url : undefined,
            text: authorText,
            isCustomized: false
          };
        }
      default:
        {
          const user = getUser(template.author);
          return {
            type: 'user',
            icon: _icons.commentAuthorAvatar,
            imageUrl: user?.avatar_urls?.[48],
            text: authorText,
            isCustomized: false
          };
        }
    }
  }, [postType, postId]);
}
//# sourceMappingURL=hooks.js.map