"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _clsx = _interopRequireDefault(require("clsx"));
var _blockEditor = require("@wordpress/block-editor");
var _element = require("@wordpress/element");
var _data = require("@wordpress/data");
var _apiFetch = _interopRequireDefault(require("@wordpress/api-fetch"));
var _url = require("@wordpress/url");
var _i18n = require("@wordpress/i18n");
var _coreData = require("@wordpress/core-data");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * External dependencies
 */

/**
 * WordPress dependencies
 */

function PostCommentsLinkEdit({
  context,
  attributes,
  setAttributes
}) {
  const {
    textAlign
  } = attributes;
  const {
    postType,
    postId
  } = context;
  const [commentsCount, setCommentsCount] = (0, _element.useState)();
  const blockProps = (0, _blockEditor.useBlockProps)({
    className: (0, _clsx.default)({
      [`has-text-align-${textAlign}`]: textAlign
    })
  });
  (0, _element.useEffect)(() => {
    if (!postId) {
      return;
    }
    const currentPostId = postId;
    (0, _apiFetch.default)({
      path: (0, _url.addQueryArgs)('/wp/v2/comments', {
        post: postId
      }),
      parse: false
    }).then(res => {
      // Stale requests will have the `currentPostId` of an older closure.
      if (currentPostId === postId) {
        setCommentsCount(res.headers.get('X-WP-Total'));
      }
    });
  }, [postId]);
  const post = (0, _data.useSelect)(select => select(_coreData.store).getEditedEntityRecord('postType', postType, postId), [postType, postId]);
  if (!post) {
    return null;
  }
  const {
    link
  } = post;
  let commentsText;
  if (commentsCount !== undefined) {
    const commentsNumber = parseInt(commentsCount);
    if (commentsNumber === 0) {
      commentsText = (0, _i18n.__)('No comments');
    } else {
      commentsText = (0, _i18n.sprintf)( /* translators: %s: Number of comments */
      (0, _i18n._n)('%s comment', '%s comments', commentsNumber), commentsNumber.toLocaleString());
    }
  }
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.BlockControls, {
      group: "block",
      children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.AlignmentControl, {
        value: textAlign,
        onChange: nextAlign => {
          setAttributes({
            textAlign: nextAlign
          });
        }
      })
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
      ...blockProps,
      children: link && commentsText !== undefined ? /*#__PURE__*/(0, _jsxRuntime.jsx)("a", {
        href: link + '#comments',
        onClick: event => event.preventDefault(),
        children: commentsText
      }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockEditor.Warning, {
        children: (0, _i18n.__)('Post Comments Link block: post not found.')
      })
    })]
  });
}
var _default = exports.default = PostCommentsLinkEdit;
//# sourceMappingURL=edit.js.map