"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = PostTemplatePanel;
var _data = require("@wordpress/data");
var _i18n = require("@wordpress/i18n");
var _coreData = require("@wordpress/core-data");
var _store = require("../../store");
var _classicTheme = _interopRequireDefault(require("./classic-theme"));
var _blockTheme = _interopRequireDefault(require("./block-theme"));
var _postPanelRow = _interopRequireDefault(require("../post-panel-row"));
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

/**
 * Displays the template controls based on the current editor settings and user permissions.
 *
 * @return {React.ReactNode} The rendered PostTemplatePanel component.
 */function PostTemplatePanel() {
  const {
    templateId,
    isBlockTheme
  } = (0, _data.useSelect)(select => {
    const {
      getCurrentTemplateId,
      getEditorSettings
    } = select(_store.store);
    return {
      templateId: getCurrentTemplateId(),
      isBlockTheme: getEditorSettings().__unstableIsBlockBasedTheme
    };
  }, []);
  const isVisible = (0, _data.useSelect)(select => {
    var _select$canUser;
    const postTypeSlug = select(_store.store).getCurrentPostType();
    const postType = select(_coreData.store).getPostType(postTypeSlug);
    if (!postType?.viewable) {
      return false;
    }
    const settings = select(_store.store).getEditorSettings();
    const hasTemplates = !!settings.availableTemplates && Object.keys(settings.availableTemplates).length > 0;
    if (hasTemplates) {
      return true;
    }
    if (!settings.supportsTemplateMode) {
      return false;
    }
    const canCreateTemplates = (_select$canUser = select(_coreData.store).canUser('create', {
      kind: 'postType',
      name: 'wp_template'
    })) !== null && _select$canUser !== void 0 ? _select$canUser : false;
    return canCreateTemplates;
  }, []);
  const canViewTemplates = (0, _data.useSelect)(select => {
    var _select$canUser2;
    return (_select$canUser2 = select(_coreData.store).canUser('read', {
      kind: 'postType',
      name: 'wp_template'
    })) !== null && _select$canUser2 !== void 0 ? _select$canUser2 : false;
  }, []);
  if ((!isBlockTheme || !canViewTemplates) && isVisible) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_postPanelRow.default, {
      label: (0, _i18n.__)('Template'),
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_classicTheme.default, {})
    });
  }
  if (isBlockTheme && !!templateId) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)(_postPanelRow.default, {
      label: (0, _i18n.__)('Template'),
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockTheme.default, {
        id: templateId
      })
    });
  }
  return null;
}
//# sourceMappingURL=panel.js.map