"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = QueryTitleEdit;
var _clsx = _interopRequireDefault(require("clsx"));
var _blockEditor = require("@wordpress/block-editor");
var _components = require("@wordpress/components");
var _i18n = require("@wordpress/i18n");
var _useArchiveLabel = require("./use-archive-label");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * External dependencies
 */

/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const SUPPORTED_TYPES = ['archive', 'search'];
function QueryTitleEdit({
  attributes: {
    type,
    level,
    levelOptions,
    textAlign,
    showPrefix,
    showSearchTerm
  },
  setAttributes
}) {
  const {
    archiveTypeLabel,
    archiveNameLabel
  } = (0, _useArchiveLabel.useArchiveLabel)();
  const TagName = `h${level}`;
  const blockProps = (0, _blockEditor.useBlockProps)({
    className: (0, _clsx.default)('wp-block-query-title__placeholder', {
      [`has-text-align-${textAlign}`]: textAlign
    })
  });
  if (!SUPPORTED_TYPES.includes(type)) {
    return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      ...blockProps,
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.Warning, {
        children: (0, _i18n.__)('Provided type is not supported.')
      })
    });
  }
  let titleElement;
  if (type === 'archive') {
    let title;
    if (archiveTypeLabel) {
      if (showPrefix) {
        if (archiveNameLabel) {
          title = (0, _i18n.sprintf)( /* translators: 1: Archive type title e.g: "Category", 2: Label of the archive e.g: "Shoes" */
          (0, _i18n.__)('%1$s: %2$s'), archiveTypeLabel, archiveNameLabel);
        } else {
          title = (0, _i18n.sprintf)( /* translators: %s: Archive type title e.g: "Category", "Tag"... */
          (0, _i18n.__)('%s: Name'), archiveTypeLabel);
        }
      } else if (archiveNameLabel) {
        title = archiveNameLabel;
      } else {
        title = (0, _i18n.sprintf)( /* translators: %s: Archive type title e.g: "Category", "Tag"... */
        (0, _i18n.__)('%s name'), archiveTypeLabel);
      }
    } else {
      title = showPrefix ? (0, _i18n.__)('Archive type: Name') : (0, _i18n.__)('Archive title');
    }
    titleElement = /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.InspectorControls, {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.PanelBody, {
          title: (0, _i18n.__)('Settings'),
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.ToggleControl, {
            __nextHasNoMarginBottom: true,
            label: (0, _i18n.__)('Show archive type in title'),
            onChange: () => setAttributes({
              showPrefix: !showPrefix
            }),
            checked: showPrefix
          })
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(TagName, {
        ...blockProps,
        children: title
      })]
    });
  }
  if (type === 'search') {
    titleElement = /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.InspectorControls, {
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.PanelBody, {
          title: (0, _i18n.__)('Settings'),
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.ToggleControl, {
            __nextHasNoMarginBottom: true,
            label: (0, _i18n.__)('Show search term in title'),
            onChange: () => setAttributes({
              showSearchTerm: !showSearchTerm
            }),
            checked: showSearchTerm
          })
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(TagName, {
        ...blockProps,
        children: showSearchTerm ? (0, _i18n.__)('Search results for: “search term”') : (0, _i18n.__)('Search results')
      })]
    });
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_blockEditor.BlockControls, {
      group: "block",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.HeadingLevelDropdown, {
        value: level,
        options: levelOptions,
        onChange: newLevel => setAttributes({
          level: newLevel
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.AlignmentControl, {
        value: textAlign,
        onChange: nextAlign => {
          setAttributes({
            textAlign: nextAlign
          });
        }
      })]
    }), titleElement]
  });
}
//# sourceMappingURL=edit.js.map