// packages/editor/src/components/post-revisions-panel/index.js
import {
  PanelBody,
  Button,
  __experimentalHStack as HStack,
  __experimentalVStack as VStack,
  privateApis as componentsPrivateApis
} from "@wordpress/components";
import { store as coreStore } from "@wordpress/core-data";
import { DataViews } from "@wordpress/dataviews";
import { dateI18n, getDate, humanTimeDiff, getSettings } from "@wordpress/date";
import { useSelect, useDispatch } from "@wordpress/data";
import { __ } from "@wordpress/i18n";
import { authorField } from "@wordpress/fields";
import PostLastRevisionCheck from "../post-last-revision/check.mjs";
import { store as editorStore } from "../../store/index.mjs";
import { unlock } from "../../lock-unlock.mjs";
import { jsx, jsxs } from "react/jsx-runtime";
var { Badge: WCBadge } = unlock(componentsPrivateApis);
var DAY_IN_MILLISECONDS = 864e5;
var EMPTY_ARRAY = [];
var defaultLayouts = { activity: true };
var noop = () => {
};
var paginationInfo = {};
var view = {
  type: "activity",
  titleField: "date",
  fields: ["author"],
  layout: { density: "compact" }
};
var fields = [
  {
    id: "date",
    label: __("Date"),
    render: ({ item, field }) => {
      const dateNowInMs = getDate(null).getTime();
      const _value = field.getValue({ item });
      const date = getDate(_value ?? null);
      const displayDate = dateNowInMs - date.getTime() > DAY_IN_MILLISECONDS ? dateI18n(
        getSettings().formats.datetimeAbbreviated,
        date
      ) : humanTimeDiff(date);
      return /* @__PURE__ */ jsx(
        "time",
        {
          className: "editor-post-revisions-panel__revision-date",
          dateTime: _value,
          children: displayDate
        }
      );
    },
    enableSorting: false,
    enableHiding: false
  },
  authorField
];
function PostRevisionsPanelContent() {
  const { setCurrentRevisionId } = unlock(useDispatch(editorStore));
  const {
    revisionsCount,
    revisions,
    revisionKey,
    isLoading,
    lastRevisionId
  } = useSelect((select) => {
    const { getCurrentPostId, getCurrentPostType } = select(editorStore);
    const { getCurrentPostRevisionsCount, getCurrentPostLastRevisionId } = select(editorStore);
    const { getRevisions, getEntityConfig, isResolving } = select(coreStore);
    const _postType = getCurrentPostType();
    const entityConfig = getEntityConfig("postType", _postType);
    const _revisionKey = entityConfig?.revisionKey || "id";
    const revisionsQuery = {
      per_page: 3,
      orderby: "date",
      order: "desc",
      _fields: `${_revisionKey},date,author`
    };
    const query = [
      "postType",
      _postType,
      getCurrentPostId(),
      revisionsQuery
    ];
    const _revisions = getRevisions(...query);
    return {
      revisionsCount: getCurrentPostRevisionsCount(),
      lastRevisionId: getCurrentPostLastRevisionId(),
      revisions: _revisions,
      revisionKey: _revisionKey,
      isLoading: isResolving("getRevisions", query)
    };
  }, []);
  return /* @__PURE__ */ jsx(
    PanelBody,
    {
      title: /* @__PURE__ */ jsxs(HStack, { justify: "space-between", align: "center", as: "span", children: [
        /* @__PURE__ */ jsx("span", { children: __("Revisions") }),
        /* @__PURE__ */ jsx(WCBadge, { className: "editor-post-revisions-panel__revisions-count", children: revisionsCount })
      ] }),
      initialOpen: false,
      children: /* @__PURE__ */ jsxs(VStack, { className: "editor-post-revisions-panel", children: [
        /* @__PURE__ */ jsx(
          DataViews,
          {
            view,
            onChangeView: noop,
            fields,
            data: revisions || EMPTY_ARRAY,
            isLoading,
            paginationInfo,
            defaultLayouts,
            getItemId: (item) => item[revisionKey],
            isItemClickable: () => true,
            onClickItem: (item) => {
              setCurrentRevisionId(item[revisionKey]);
            },
            children: /* @__PURE__ */ jsx(DataViews.Layout, {})
          }
        ),
        /* @__PURE__ */ jsx(
          Button,
          {
            className: "editor-post-revisions-panel__view-all",
            __next40pxDefaultSize: true,
            variant: "secondary",
            onClick: () => setCurrentRevisionId(lastRevisionId),
            children: __("View all revisions")
          }
        )
      ] })
    }
  );
}
function PostRevisionsPanel() {
  return /* @__PURE__ */ jsx(PostLastRevisionCheck, { children: /* @__PURE__ */ jsx(PostRevisionsPanelContent, {}) });
}
export {
  PostRevisionsPanel as default
};
//# sourceMappingURL=index.mjs.map
