/* wp:polyfill */
"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.PrivatePostSchedule = PrivatePostSchedule;
exports.default = PostSchedule;
var _dateFns = require("date-fns");
var _date = require("@wordpress/date");
var _i18n = require("@wordpress/i18n");
var _data = require("@wordpress/data");
var _blockEditor = require("@wordpress/block-editor");
var _element = require("@wordpress/element");
var _coreData = require("@wordpress/core-data");
var _store = require("../../store");
var _lockUnlock = require("../../lock-unlock");
var _jsxRuntime = require("react/jsx-runtime");
/**
 * External dependencies
 */

/**
 * WordPress dependencies
 */

/**
 * Internal dependencies
 */

const {
  PrivatePublishDateTimePicker
} = (0, _lockUnlock.unlock)(_blockEditor.privateApis);

/**
 * Renders the PostSchedule component. It allows the user to schedule a post.
 *
 * @param {Object}   props         Props.
 * @param {Function} props.onClose Function to close the component.
 *
 * @return {React.ReactNode} The rendered component.
 */
function PostSchedule(props) {
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(PrivatePostSchedule, {
    ...props,
    showPopoverHeaderActions: true,
    isCompact: false
  });
}
function PrivatePostSchedule({
  onClose,
  showPopoverHeaderActions,
  isCompact
}) {
  const {
    postDate,
    postType
  } = (0, _data.useSelect)(select => ({
    postDate: select(_store.store).getEditedPostAttribute('date'),
    postType: select(_store.store).getCurrentPostType()
  }), []);
  const {
    editPost
  } = (0, _data.useDispatch)(_store.store);
  const onUpdateDate = date => editPost({
    date
  });
  const [previewedMonth, setPreviewedMonth] = (0, _element.useState)((0, _dateFns.startOfMonth)(new Date(postDate)));

  // Pick up published and scheduled site posts.
  const eventsByPostType = (0, _data.useSelect)(select => select(_coreData.store).getEntityRecords('postType', postType, {
    status: 'publish,future',
    after: (0, _dateFns.startOfMonth)(previewedMonth).toISOString(),
    before: (0, _dateFns.endOfMonth)(previewedMonth).toISOString(),
    exclude: [select(_store.store).getCurrentPostId()],
    per_page: 100,
    _fields: 'id,date'
  }), [previewedMonth, postType]);
  const events = (0, _element.useMemo)(() => (eventsByPostType || []).map(({
    date: eventDate
  }) => ({
    date: new Date(eventDate)
  })), [eventsByPostType]);
  const settings = (0, _date.getSettings)();

  // To know if the current timezone is a 12 hour time with look for "a" in the time format
  // We also make sure this a is not escaped by a "/"
  const is12HourTime = /a(?!\\)/i.test(settings.formats.time.toLowerCase() // Test only the lower case a.
  .replace(/\\\\/g, '') // Replace "//" with empty strings.
  .split('').reverse().join('') // Reverse the string and test for "a" not followed by a slash.
  );
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(PrivatePublishDateTimePicker, {
    currentDate: postDate,
    onChange: onUpdateDate,
    is12Hour: is12HourTime,
    dateOrder: /* translators: Order of day, month, and year. Available formats are 'dmy', 'mdy', and 'ymd'. */
    (0, _i18n._x)('dmy', 'date order'),
    events: events,
    onMonthPreviewed: date => setPreviewedMonth((0, _dateFns.parseISO)(date)),
    onClose: onClose,
    isCompact: isCompact,
    showPopoverHeaderActions: showPopoverHeaderActions
  });
}
//# sourceMappingURL=index.js.map