/*
THIS IS A GENERATED/BUNDLED FILE BY ESBUILD
if you want to view the source, please visit the github repository of this plugin
*/

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);

// src/main.ts
var main_exports = {};
__export(main_exports, {
  default: () => Mehrmaid
});
module.exports = __toCommonJS(main_exports);
var import_obsidian2 = require("obsidian");

// src/mermaid.ts
var import_obsidian = require("obsidian");

// src/themes.ts
var THEME_DARK = {
  theme: "dark",
  "themeVariables": {
    "primaryColor": "#BB2528",
    "primaryTextColor": "#fff",
    "primaryBorderColor": "#7C0000",
    "lineColor": "#dadada",
    "secondaryColor": "#006100",
    "tertiaryColor": "#fff",
    "clusterBkg": "#242424",
    "clusterBorder": "#2d2d2d",
    "nodeBorder": "#dadada"
  },
  securityLevel: "loose",
  startOnLoad: true,
  flowchart: {
    htmlLabels: true
  }
};
var THEME_LIGHT = {
  theme: "neutral",
  "themeVariables": {
    "primaryBorderColor": "#000",
    "clusterBorder": "#2d2d2d",
    "lineColor": "#000",
    "nodeBorder": "#000"
  },
  securityLevel: "loose",
  startOnLoad: true,
  flowchart: {
    htmlLabels: true
  }
};

// src/mermaid.ts
async function renderMarkdown(str, el, ctx, app) {
  const markdownRenderChild = new import_obsidian.MarkdownRenderChild(el);
  const markdownEl = el.createDiv();
  markdownEl.addClass("mehrmaid-markdown-container");
  if (ctx && !(typeof ctx == "string")) {
    ctx.addChild(markdownRenderChild);
  }
  await import_obsidian.MarkdownRenderer.render(app, str, markdownEl, ctx.sourcePath, markdownRenderChild);
  return markdownEl;
}
async function renderMehrmaid(source, el, ctx) {
  var _a;
  const mermaid = await (0, import_obsidian.loadMermaid)();
  let config = {};
  if (document.body.classList.contains("theme-dark")) {
    config = THEME_DARK;
    source += "\nclassDef primary fill:#8a5cf5";
  } else {
    config = THEME_LIGHT;
    source += "\nclassDef primary fill:#a68afa";
  }
  mermaid.initialize(config);
  mermaid.mermaidAPI.setConfig(config);
  let matches = source.match(/"([^"]*?)"/g);
  if (matches) {
    const promises = [];
    for (let match of matches) {
      match = match.substring(1, match.length - 1);
      promises.push(renderMarkdown(match, el, ctx, this.app));
    }
    const markdownEls = await Promise.all(promises);
    let widths = [];
    let heights = [];
    for (let markdownEl of markdownEls) {
      widths.push(markdownEl.offsetWidth);
      heights.push(markdownEl.offsetHeight);
    }
    el.empty();
    for (let i = 0; i < matches.length; i++) {
      let match = matches[i];
      let markdownEl = markdownEls[i];
      let width = widths[i];
      width = Math.max(width, 10);
      let height = heights[i];
      height = Math.max(height, 10);
      let id = match.replace(/[^a-zA-Z0-9]/g, "") + i;
      source = source.replace(
        match,
        `<div class="${id} cm-sizer" style="width: ${width}px; height: ${height - 7}px; display: inline-block;"></div>`
      );
    }
    const graphId = "mehrmaid-" + ((_a = ctx.getSectionInfo(el)) == null ? void 0 : _a.lineStart) + "-" + Date.now();
    const { svg } = await mermaid.render(graphId, source);
    el.insertAdjacentHTML("beforeend", svg);
    for (let i = 0; i < markdownEls.length; i++) {
      let id = matches[i].replace(/[^a-zA-Z0-9]/g, "") + i;
      let markdownEl = markdownEls[i];
      let htmlEl = el.getElementsByClassName(id)[0];
      htmlEl.appendChild(markdownEl);
    }
  }
}

// src/main.ts
var Mehrmaid = class extends import_obsidian2.Plugin {
  async onload() {
    this.app.workspace.onLayoutReady(() => {
      this.registerMarkdownCodeBlockProcessor("mehrmaid", async (source, el, ctx) => {
        try {
          await renderMehrmaid(source, el, ctx);
        } catch (e) {
          console.error(e);
          new import_obsidian2.Notice("Mehrmaid: Error rendering mermaid diagram");
        }
      });
    });
  }
};

/* nosourcemap */