"use strict";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// packages/block-editor/src/components/rich-text/multiline.js
var multiline_exports = {};
__export(multiline_exports, {
  default: () => multiline_default
});
module.exports = __toCommonJS(multiline_exports);
var import_element = require("@wordpress/element");
var import_deprecated = __toESM(require("@wordpress/deprecated"));
var import_data = require("@wordpress/data");
var import_keycodes = require("@wordpress/keycodes");
var import_rich_text = require("@wordpress/rich-text");
var import__ = require("./");
var import_store = require("../../store");
var import_block_edit = require("../block-edit");
var import_utils = require("./utils");
var import_jsx_runtime = require("react/jsx-runtime");
function RichTextMultiline({
  children,
  identifier,
  tagName: TagName = "div",
  value = "",
  onChange,
  multiline,
  ...props
}, forwardedRef) {
  (0, import_deprecated.default)("wp.blockEditor.RichText multiline prop", {
    since: "6.1",
    version: "6.3",
    alternative: "nested blocks (InnerBlocks)",
    link: "https://developer.wordpress.org/block-editor/how-to-guides/block-tutorial/nested-blocks-inner-blocks/"
  });
  const { clientId } = (0, import_block_edit.useBlockEditContext)();
  const { getSelectionStart, getSelectionEnd } = (0, import_data.useSelect)(import_store.store);
  const { selectionChange } = (0, import_data.useDispatch)(import_store.store);
  const multilineTagName = (0, import_utils.getMultilineTag)(multiline);
  value = value || `<${multilineTagName}></${multilineTagName}>`;
  const padded = `</${multilineTagName}>${value}<${multilineTagName}>`;
  const values = padded.split(
    `</${multilineTagName}><${multilineTagName}>`
  );
  values.shift();
  values.pop();
  function _onChange(newValues) {
    onChange(
      `<${multilineTagName}>${newValues.join(
        `</${multilineTagName}><${multilineTagName}>`
      )}</${multilineTagName}>`
    );
  }
  return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(TagName, { ref: forwardedRef, children: values.map((_value, index) => {
    return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
      import__.RichTextWrapper,
      {
        identifier: `${identifier}-${index}`,
        tagName: multilineTagName,
        value: _value,
        onChange: (newValue) => {
          const newValues = values.slice();
          newValues[index] = newValue;
          _onChange(newValues);
        },
        isSelected: void 0,
        onKeyDown: (event) => {
          if (event.keyCode !== import_keycodes.ENTER) {
            return;
          }
          event.preventDefault();
          const { offset: start } = getSelectionStart();
          const { offset: end } = getSelectionEnd();
          if (typeof start !== "number" || typeof end !== "number") {
            return;
          }
          const richTextValue = (0, import_rich_text.create)({ html: _value });
          richTextValue.start = start;
          richTextValue.end = end;
          const array = (0, import_rich_text.split)(richTextValue).map(
            (v) => (0, import_rich_text.toHTMLString)({ value: v })
          );
          const newValues = values.slice();
          newValues.splice(index, 1, ...array);
          _onChange(newValues);
          selectionChange(
            clientId,
            `${identifier}-${index + 1}`,
            0,
            0
          );
        },
        onMerge: (forward) => {
          const newValues = values.slice();
          let offset = 0;
          if (forward) {
            if (!newValues[index + 1]) {
              return;
            }
            newValues.splice(
              index,
              2,
              newValues[index] + newValues[index + 1]
            );
            offset = newValues[index].length - 1;
          } else {
            if (!newValues[index - 1]) {
              return;
            }
            newValues.splice(
              index - 1,
              2,
              newValues[index - 1] + newValues[index]
            );
            offset = newValues[index - 1].length - 1;
          }
          _onChange(newValues);
          selectionChange(
            clientId,
            `${identifier}-${index - (forward ? 0 : 1)}`,
            offset,
            offset
          );
        },
        ...props
      },
      index
    );
  }) });
}
var multiline_default = (0, import_element.forwardRef)(RichTextMultiline);
//# sourceMappingURL=multiline.js.map
