// packages/editor/src/components/post-text-editor/index.js
import Textarea from "react-autosize-textarea";
import { __ } from "@wordpress/i18n";
import { store as coreStore } from "@wordpress/core-data";
import { useMemo } from "@wordpress/element";
import { __unstableSerializeAndClean } from "@wordpress/blocks";
import { useDispatch, useSelect } from "@wordpress/data";
import { useInstanceId } from "@wordpress/compose";
import { VisuallyHidden } from "@wordpress/components";
import { store as editorStore } from "../../store/index.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
function PostTextEditor() {
  const instanceId = useInstanceId(PostTextEditor);
  const { content, blocks, type, id } = useSelect((select) => {
    const { getEditedEntityRecord } = select(coreStore);
    const { getCurrentPostType, getCurrentPostId } = select(editorStore);
    const _type = getCurrentPostType();
    const _id = getCurrentPostId();
    const editedRecord = getEditedEntityRecord("postType", _type, _id);
    return {
      content: editedRecord?.content,
      blocks: editedRecord?.blocks,
      type: _type,
      id: _id
    };
  }, []);
  const { editEntityRecord } = useDispatch(coreStore);
  const value = useMemo(() => {
    if (content instanceof Function) {
      return content({ blocks });
    } else if (blocks) {
      return __unstableSerializeAndClean(blocks);
    }
    return content;
  }, [content, blocks]);
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(
      VisuallyHidden,
      {
        as: "label",
        htmlFor: `post-content-${instanceId}`,
        children: __("Type text or HTML")
      }
    ),
    /* @__PURE__ */ jsx(
      Textarea,
      {
        autoComplete: "off",
        dir: "auto",
        value,
        onChange: (event) => {
          editEntityRecord("postType", type, id, {
            content: event.target.value,
            blocks: void 0,
            selection: void 0
          });
        },
        className: "editor-post-text-editor",
        id: `post-content-${instanceId}`,
        placeholder: __("Start writing with text or HTML")
      }
    )
  ] });
}
export {
  PostTextEditor as default
};
//# sourceMappingURL=index.mjs.map
