"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = PostPublishPanelPostpublish;
var _components = require("@wordpress/components");
var _i18n = require("@wordpress/i18n");
var _element = require("@wordpress/element");
var _data = require("@wordpress/data");
var _url = require("@wordpress/url");
var _htmlEntities = require("@wordpress/html-entities");
var _compose = require("@wordpress/compose");
var _coreData = require("@wordpress/core-data");
var _icons = require("@wordpress/icons");
var _label = _interopRequireDefault(require("../post-schedule/label"));
var _store = require("../../store");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const POSTNAME = '%postname%';
const PAGENAME = '%pagename%';

/**
 * Returns URL for a future post.
 *
 * @param {Object} post Post object.
 *
 * @return {string} PostPublish URL.
 */

const getFuturePostUrl = post => {
  const {
    slug
  } = post;
  if (post.permalink_template.includes(POSTNAME)) {
    return post.permalink_template.replace(POSTNAME, slug);
  }
  if (post.permalink_template.includes(PAGENAME)) {
    return post.permalink_template.replace(PAGENAME, slug);
  }
  return post.permalink_template;
};
function CopyButton({
  text
}) {
  const [showCopyConfirmation, setShowCopyConfirmation] = (0, _element.useState)(false);
  const timeoutIdRef = (0, _element.useRef)();
  const ref = (0, _compose.useCopyToClipboard)(text, () => {
    setShowCopyConfirmation(true);
    if (timeoutIdRef.current) {
      clearTimeout(timeoutIdRef.current);
    }
    timeoutIdRef.current = setTimeout(() => {
      setShowCopyConfirmation(false);
    }, 4000);
  });
  (0, _element.useEffect)(() => {
    return () => {
      if (timeoutIdRef.current) {
        clearTimeout(timeoutIdRef.current);
      }
    };
  }, []);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
    __next40pxDefaultSize: true,
    variant: "secondary",
    ref: ref,
    children: showCopyConfirmation ? (0, _i18n.__)('Copied!') : (0, _i18n.__)('Copy')
  });
}
function PostPublishPanelPostpublish({
  focusOnMount,
  children
}) {
  const {
    post,
    postType,
    isScheduled
  } = (0, _data.useSelect)(select => {
    const {
      getEditedPostAttribute,
      getCurrentPost,
      isCurrentPostScheduled
    } = select(_store.store);
    const {
      getPostType
    } = select(_coreData.store);
    return {
      post: getCurrentPost(),
      postType: getPostType(getEditedPostAttribute('type')),
      isScheduled: isCurrentPostScheduled()
    };
  }, []);
  const postLabel = postType?.labels?.singular_name;
  const viewPostLabel = postType?.labels?.view_item;
  const addNewPostLabel = postType?.labels?.add_new_item;
  const link = post.status === 'future' ? getFuturePostUrl(post) : post.link;
  const addLink = (0, _url.addQueryArgs)('post-new.php', {
    post_type: post.type
  });
  const postLinkRef = (0, _element.useCallback)(node => {
    if (focusOnMount && node) {
      node.focus();
    }
  }, [focusOnMount]);
  const postPublishNonLinkHeader = isScheduled ? /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
    children: [(0, _i18n.__)('is now scheduled. It will go live on'), ' ', /*#__PURE__*/(0, _jsxRuntime.jsx)(_label.default, {}), "."]
  }) : (0, _i18n.__)('is now live.');
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: "post-publish-panel__postpublish",
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.PanelBody, {
      className: "post-publish-panel__postpublish-header",
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.ExternalLink, {
        ref: postLinkRef,
        href: link,
        children: (0, _htmlEntities.decodeEntities)(post.title) || (0, _i18n.__)('(no title)')
      }), ' ', postPublishNonLinkHeader]
    }), /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.PanelBody, {
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("p", {
        className: "post-publish-panel__postpublish-subheader",
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)("strong", {
          children: (0, _i18n.__)('What’s next?')
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: "post-publish-panel__postpublish-post-address-container",
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_components.TextControl, {
          __next40pxDefaultSize: true,
          __nextHasNoMarginBottom: true,
          className: "post-publish-panel__postpublish-post-address",
          readOnly: true,
          label: (0, _i18n.sprintf)(/* translators: %s: post type singular name */
          (0, _i18n.__)('%s address'), postLabel),
          value: (0, _url.safeDecodeURIComponent)(link),
          onFocus: event => event.target.select()
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "post-publish-panel__postpublish-post-address__copy-button-wrap",
          children: /*#__PURE__*/(0, _jsxRuntime.jsx)(CopyButton, {
            text: link
          })
        })]
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: "post-publish-panel__postpublish-buttons",
        children: [!isScheduled && /*#__PURE__*/(0, _jsxRuntime.jsxs)(_components.Button, {
          variant: "primary",
          href: link,
          __next40pxDefaultSize: true,
          icon: _icons.external,
          iconPosition: "right",
          target: "_blank",
          children: [viewPostLabel, /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.VisuallyHidden, {
            as: "span",
            children: /* translators: accessibility text */
            (0, _i18n.__)('(opens in a new tab)')
          })]
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_components.Button, {
          variant: isScheduled ? 'primary' : 'secondary',
          __next40pxDefaultSize: true,
          href: addLink,
          children: addNewPostLabel
        })]
      })]
    }), children]
  });
}
//# sourceMappingURL=postpublish.js.map