// packages/block-library/src/comment-date/edit.js
import { useEntityProp } from "@wordpress/core-data";
import {
  dateI18n,
  humanTimeDiff,
  getSettings as getDateSettings
} from "@wordpress/date";
import {
  InspectorControls,
  useBlockProps,
  __experimentalDateFormatPicker as DateFormatPicker
} from "@wordpress/block-editor";
import {
  __experimentalToolsPanel as ToolsPanel,
  __experimentalToolsPanelItem as ToolsPanelItem,
  ToggleControl
} from "@wordpress/components";
import { __, _x } from "@wordpress/i18n";
import { useToolsPanelDropdownMenuProps } from "../utils/hooks";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
function Edit({
  attributes: { format, isLink },
  context: { commentId },
  setAttributes
}) {
  const blockProps = useBlockProps();
  const dropdownMenuProps = useToolsPanelDropdownMenuProps();
  let [date] = useEntityProp("root", "comment", "date", commentId);
  const [siteFormat = getDateSettings().formats.date] = useEntityProp(
    "root",
    "site",
    "date_format"
  );
  const inspectorControls = /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(
    ToolsPanel,
    {
      label: __("Settings"),
      resetAll: () => {
        setAttributes({
          format: void 0,
          isLink: true
        });
      },
      dropdownMenuProps,
      children: [
        /* @__PURE__ */ jsx(
          ToolsPanelItem,
          {
            label: __("Date format"),
            hasValue: () => format !== void 0,
            onDeselect: () => setAttributes({ format: void 0 }),
            isShownByDefault: true,
            children: /* @__PURE__ */ jsx(
              DateFormatPicker,
              {
                format,
                defaultFormat: siteFormat,
                onChange: (nextFormat) => setAttributes({ format: nextFormat })
              }
            )
          }
        ),
        /* @__PURE__ */ jsx(
          ToolsPanelItem,
          {
            label: __("Link to comment"),
            hasValue: () => !isLink,
            onDeselect: () => setAttributes({ isLink: true }),
            isShownByDefault: true,
            children: /* @__PURE__ */ jsx(
              ToggleControl,
              {
                __nextHasNoMarginBottom: true,
                label: __("Link to comment"),
                onChange: () => setAttributes({ isLink: !isLink }),
                checked: isLink
              }
            )
          }
        )
      ]
    }
  ) });
  if (!commentId || !date) {
    date = _x("Comment Date", "block title");
  }
  let commentDate = date instanceof Date ? /* @__PURE__ */ jsx("time", { dateTime: dateI18n("c", date), children: format === "human-diff" ? humanTimeDiff(date) : dateI18n(format || siteFormat, date) }) : /* @__PURE__ */ jsx("time", { children: date });
  if (isLink) {
    commentDate = /* @__PURE__ */ jsx(
      "a",
      {
        href: "#comment-date-pseudo-link",
        onClick: (event) => event.preventDefault(),
        children: commentDate
      }
    );
  }
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    inspectorControls,
    /* @__PURE__ */ jsx("div", { ...blockProps, children: commentDate })
  ] });
}
export {
  Edit as default
};
//# sourceMappingURL=edit.js.map
