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