// packages/block-library/src/post-time-to-read/edit.js
import { __, _x, _n, sprintf } from "@wordpress/i18n";
import { useMemo } from "@wordpress/element";
import { useBlockProps, InspectorControls } from "@wordpress/block-editor";
import {
  ToggleControl,
  __experimentalToolsPanel as ToolsPanel,
  __experimentalToolsPanelItem as ToolsPanelItem
} from "@wordpress/components";
import { __unstableSerializeAndClean } from "@wordpress/blocks";
import { useEntityProp, useEntityBlockEditor } from "@wordpress/core-data";
import { count as wordCount } from "@wordpress/wordcount";
import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
function PostTimeToReadEdit({ attributes, setAttributes, context }) {
  const { displayAsRange, displayMode, averageReadingSpeed } = attributes;
  const { postId, postType } = context;
  const dropdownMenuProps = useToolsPanelDropdownMenuProps();
  const [contentStructure] = useEntityProp(
    "postType",
    postType,
    "content",
    postId
  );
  const [blocks] = useEntityBlockEditor("postType", postType, {
    id: postId
  });
  const displayString = useMemo(() => {
    let content;
    if (contentStructure instanceof Function) {
      content = contentStructure({ blocks });
    } else if (blocks) {
      content = __unstableSerializeAndClean(blocks);
    } else {
      content = contentStructure;
    }
    const wordCountType = _x(
      "words",
      "Word count type. Do not translate!"
    );
    const totalWords = wordCount(content || "", wordCountType);
    if (displayMode === "time") {
      if (displayAsRange) {
        let maxMinutes = Math.max(
          1,
          Math.round(totalWords / averageReadingSpeed * 1.2)
        );
        const minMinutes = Math.max(
          1,
          Math.round(totalWords / averageReadingSpeed * 0.8)
        );
        if (minMinutes === maxMinutes) {
          maxMinutes = maxMinutes + 1;
        }
        const rangeLabel = _x(
          "%1$s–%2$s minutes",
          "Range of minutes to read"
        );
        return sprintf(rangeLabel, minMinutes, maxMinutes);
      }
      const minutesToRead = Math.max(
        1,
        Math.round(totalWords / averageReadingSpeed)
      );
      return sprintf(
        /* translators: %s: the number of minutes to read the post. */
        _n("%s minute", "%s minutes", minutesToRead),
        minutesToRead
      );
    }
    if (displayMode === "words") {
      return wordCountType === "words" ? sprintf(
        /* translators: %s: the number of words in the post. */
        _n("%s word", "%s words", totalWords),
        totalWords.toLocaleString()
      ) : sprintf(
        /* translators: %s: the number of characters in the post. */
        _n("%s character", "%s characters", totalWords),
        totalWords.toLocaleString()
      );
    }
  }, [
    contentStructure,
    blocks,
    displayAsRange,
    displayMode,
    averageReadingSpeed
  ]);
  const blockProps = useBlockProps();
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    displayMode === "time" && /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsx(
      ToolsPanel,
      {
        label: __("Settings"),
        resetAll: () => {
          setAttributes({
            displayAsRange: true
          });
        },
        dropdownMenuProps,
        children: /* @__PURE__ */ jsx(
          ToolsPanelItem,
          {
            isShownByDefault: true,
            label: _x(
              "Display as range",
              "Turns reading time range display on or off"
            ),
            hasValue: () => !displayAsRange,
            onDeselect: () => {
              setAttributes({
                displayAsRange: true
              });
            },
            children: /* @__PURE__ */ jsx(
              ToggleControl,
              {
                label: __("Display as range"),
                checked: !!displayAsRange,
                onChange: () => setAttributes({
                  displayAsRange: !displayAsRange
                })
              }
            )
          }
        )
      }
    ) }),
    /* @__PURE__ */ jsx("div", { ...blockProps, children: displayString })
  ] });
}
var edit_default = PostTimeToReadEdit;
export {
  edit_default as default
};
//# sourceMappingURL=edit.mjs.map
