"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var activate_caption_exports = {};
__export(activate_caption_exports, {
  activateCaption: () => activateCaption
});
module.exports = __toCommonJS(activate_caption_exports);
var import_get_text_direction = require("./../../ricos-content/get-text-direction.js");
var import_state = require("./../../tiptap/pm/state.js");
var import_helpers = require("./../../tiptap-editor/helpers/index.js");
const activateCaption = (view, captionId) => {
  const captionNodePos = (0, import_helpers.findNodeById)(view.state.tr, captionId)[0];
  if (!captionNodePos) {
    return false;
  }
  const { tr } = view.state;
  tr.setNodeAttribute(captionNodePos.pos, "showEmpty", true);
  view.dispatch(tr);
  setTimeout(() => {
    const captionNode = captionNodePos.node;
    const hasContent = captionNode.content.size > 0;
    let selectionPos = captionNodePos.pos + 1;
    if (hasContent) {
      const captionText = captionNode.textContent;
      const direction = (0, import_get_text_direction.getTextDirection)(captionText);
      selectionPos = direction === "rtl" ? selectionPos : selectionPos + captionNode.content.size;
    }
    const { tr: tr2 } = view.state;
    tr2.setSelection(import_state.TextSelection.create(tr2.doc, selectionPos, selectionPos));
    view.dispatch(tr2);
    view.focus();
  }, 0);
  return true;
};
//# sourceMappingURL=activate-caption.js.map
