"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/editor/src/components/post-revisions-preview/revisions-slider.js
var revisions_slider_exports = {};
__export(revisions_slider_exports, {
  default: () => revisions_slider_default
});
module.exports = __toCommonJS(revisions_slider_exports);
var import_data = require("@wordpress/data");
var import_components = require("@wordpress/components");
var import_core_data = require("@wordpress/core-data");
var import_i18n = require("@wordpress/i18n");
var import_date = require("@wordpress/date");
var import_element = require("@wordpress/element");
var import_icons = require("@wordpress/icons");
var import_ui = require("@wordpress/ui");
var import_store = require("../../store/index.cjs");
var import_lock_unlock = require("../../lock-unlock.cjs");
var import_jsx_runtime = require("react/jsx-runtime");
function RevisionsSlider() {
  const {
    revisions: rawRevisions,
    perPage,
    currentRevisionId,
    revisionKey,
    revisionPage,
    totalRevisions
  } = (0, import_data.useSelect)((select) => {
    const {
      getCurrentRevisionId,
      getRevisionPage,
      getPageRevisions,
      getRevisionsPerPage
    } = (0, import_lock_unlock.unlock)(select(import_store.store));
    const postType = select(import_store.store).getCurrentPostType();
    if (!postType) {
      return {};
    }
    const entityConfig = select(import_core_data.store).getEntityConfig(
      "postType",
      postType
    );
    const _revisionKey = entityConfig?.revisionKey || "id";
    const _revisionPage = getRevisionPage();
    return {
      revisions: getPageRevisions(_revisionPage),
      perPage: getRevisionsPerPage(),
      currentRevisionId: getCurrentRevisionId(),
      revisionKey: _revisionKey,
      revisionPage: _revisionPage,
      totalRevisions: select(import_store.store).getCurrentPostRevisionsCount()
    };
  }, []);
  const { setCurrentRevisionId, setRevisionPage } = (0, import_lock_unlock.unlock)(
    (0, import_data.useDispatch)(import_store.store)
  );
  const isLoading = !rawRevisions;
  const totalPages = Math.ceil(totalRevisions / perPage) || 1;
  const revisions = (0, import_element.useMemo)(
    () => rawRevisions && [...rawRevisions].reverse(),
    [rawRevisions]
  );
  const selectedIndex = revisions?.findIndex(
    (r) => r[revisionKey] === currentRevisionId
  );
  const handleSliderChange = (index) => {
    const revision = revisions?.[index];
    if (revision) {
      setCurrentRevisionId(revision[revisionKey]);
    }
  };
  const dateSettings = (0, import_date.getSettings)();
  const renderTooltipContent = (index) => {
    const revision = revisions?.[index];
    if (!revision) {
      return index;
    }
    return (0, import_date.dateI18n)(dateSettings.formats.datetime, revision.date);
  };
  const showPagination = totalPages > 1;
  if (isLoading && !showPagination) {
    return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.Spinner, {});
  }
  if (!isLoading && !revisions?.length) {
    return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "editor-revisions-header__no-revisions", children: (0, import_i18n.__)("No revisions found.") });
  }
  if (totalRevisions <= 1) {
    return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "editor-revisions-header__no-revisions", children: (0, import_i18n.__)("Only one revision found.") });
  }
  const getPageRangeLabel = (page) => {
    const end = totalRevisions - (page - 1) * perPage;
    const start = Math.max(1, end - perPage + 1);
    return (0, import_i18n.sprintf)(
      /* translators: 1: first revision number, 2: last revision number */
      (0, import_i18n.__)("Revisions %1$s\u2013%2$s"),
      start,
      end
    );
  };
  const sliderOrSpinner = isLoading || selectedIndex === -1 ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_components.Spinner, {}) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
    import_components.RangeControl,
    {
      __next40pxDefaultSize: true,
      "aria-valuetext": renderTooltipContent(selectedIndex),
      className: "editor-revisions-header__slider",
      hideLabelFromVision: true,
      label: (0, import_i18n.__)("Revision"),
      max: revisions?.length - 1,
      min: 0,
      marks: true,
      onChange: handleSliderChange,
      renderTooltipContent,
      value: selectedIndex,
      withInputField: false
    }
  );
  if (!showPagination) {
    return sliderOrSpinner;
  }
  return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ui.Stack, { direction: "row", gap: "sm", align: "center", style: { flex: 1 }, children: [
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_components.Button,
      {
        icon: import_icons.chevronLeft,
        label: revisionPage < totalPages ? getPageRangeLabel(revisionPage + 1) : (0, import_i18n.__)("No older revisions"),
        onClick: () => setRevisionPage(revisionPage + 1),
        disabled: isLoading || revisionPage >= totalPages,
        size: "compact",
        accessibleWhenDisabled: true
      }
    ),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      "div",
      {
        style: {
          flex: 1,
          minWidth: 0,
          display: "flex",
          justifyContent: "center"
        },
        children: sliderOrSpinner
      }
    ),
    /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import_components.Button,
      {
        icon: import_icons.chevronRight,
        label: revisionPage > 1 ? getPageRangeLabel(revisionPage - 1) : (0, import_i18n.__)("No newer revisions"),
        onClick: () => setRevisionPage(revisionPage - 1),
        disabled: isLoading || revisionPage <= 1,
        size: "compact",
        accessibleWhenDisabled: true
      }
    )
  ] });
}
var revisions_slider_default = RevisionsSlider;
//# sourceMappingURL=revisions-slider.cjs.map
