/*
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: () => OpenVSCode
});
module.exports = __toCommonJS(main_exports);
var import_obsidian2 = require("obsidian");

// src/settings.ts
var import_obsidian = require("obsidian");
var DEFAULT_SETTINGS = {
  ribbonIcon: true,
  ribbonCommandUsesCode: true,
  executeTemplate: 'code "{{vaultpath}}" "{{vaultpath}}/{{filepath}}"',
  openFile: true,
  workspacePath: "{{vaultpath}}",
  useUrlInsiders: false
};
var OpenVSCodeSettingsTab = class extends import_obsidian.PluginSettingTab {
  constructor(app, plugin) {
    super(app, plugin);
    this.plugin = plugin;
  }
  display() {
    const { containerEl } = this;
    containerEl.empty();
    containerEl.createEl("h3", { text: "General settings" });
    new import_obsidian.Setting(containerEl).setName("Display Ribbon Icon").setDesc("Toggle this OFF if you want to hide the Ribbon Icon.").addToggle((toggle) => toggle.setValue(this.plugin.settings.ribbonIcon).onChange((value) => {
      this.plugin.settings.ribbonIcon = value;
      this.plugin.saveSettings();
      this.plugin.refreshIconRibbon();
    }));
    new import_obsidian.Setting(containerEl).setName("Ribbon opens via `code` command").setDesc(`Toggle this OFF if you'd prefer that the Ribbon Icon opens VSCode via URL.`).addToggle((toggle) => toggle.setValue(this.plugin.settings.ribbonCommandUsesCode).onChange((value) => {
      this.plugin.settings.ribbonCommandUsesCode = value;
      this.plugin.saveSettings();
    }));
    containerEl.createEl("h3", { text: "Open via `code` CLI settings" });
    new import_obsidian.Setting(containerEl).setName("Template for executing the `code` command").setDesc('You can use the following variables: `{{vaultpath}}` (absolute), `{{filepath}}` (relative). Note that on MacOS, a full path to the VSCode executable is required (generally "/usr/local/bin/code"). Example: `/usr/local/bin/code "{{vaultpath}}" "{{vaultpath}}/{{filepath}}"`').addText((text) => text.setPlaceholder(DEFAULT_SETTINGS.executeTemplate).setValue(this.plugin.settings.executeTemplate || DEFAULT_SETTINGS.executeTemplate).onChange((value) => {
      value = value.trim();
      if (value === "")
        value = DEFAULT_SETTINGS.executeTemplate;
      this.plugin.settings.executeTemplate = value;
      this.plugin.saveData(this.plugin.settings);
    }));
    containerEl.createEl("h3", { text: "Open via `vscode://` URL settings" });
    const openViaUrlCaveat = containerEl.createEl("p");
    const openViaUrlCaveatEm = openViaUrlCaveat.createEl("em", {
      text: `
				\u26A0\uFE0F This setting is not recommended for Windows users due to
				UX issues caused by security enhancements in VSCode on Windows.
				More information:
			`
    });
    openViaUrlCaveatEm.appendChild(createEl("a", {
      text: "Open in VSCode Readme",
      href: "https://github.com/NomarCub/obsidian-open-vscode/blob/master/README.md#caveats-regarding-the-url-command-for-windows-users"
    }));
    openViaUrlCaveatEm.appendText(".");
    new import_obsidian.Setting(containerEl).setName("Open current file").setDesc("Open the current file rather than the root of the vault.").addToggle((toggle) => toggle.setValue(this.plugin.settings.openFile || DEFAULT_SETTINGS.openFile).onChange((value) => {
      this.plugin.settings.openFile = value;
      this.plugin.saveData(this.plugin.settings);
    }));
    new import_obsidian.Setting(containerEl).setName("Path to VSCode Workspace").setDesc('Defaults to the {{vaultpath}} template variable. You can set this to an absolute path to a ".code-workspace" file if you prefer to use a Multi Root workspace file: ').setClass("setting-item--vscode-workspacePath").addText((text) => text.setPlaceholder(DEFAULT_SETTINGS.workspacePath).setValue(this.plugin.settings.workspacePath || DEFAULT_SETTINGS.workspacePath).onChange((value) => {
      value = value.trim();
      if (value === "")
        value = DEFAULT_SETTINGS.workspacePath;
      this.plugin.settings.workspacePath = value;
      this.plugin.saveData(this.plugin.settings);
    }));
    const workspacePathDescEl = containerEl.querySelector(".setting-item--vscode-workspacePath .setting-item-description");
    workspacePathDescEl.appendChild(createEl("a", {
      href: "https://code.visualstudio.com/docs/editor/workspaces#_multiroot-workspaces",
      text: "https://code.visualstudio.com/docs/editor/workspaces#_multiroot-workspaces"
    }));
    workspacePathDescEl.appendText(".");
    new import_obsidian.Setting(containerEl).setName("Open VSCode using a `vscode-insiders://` URL").addToggle((toggle) => {
      toggle.setValue(this.plugin.settings.useUrlInsiders).onChange((value) => {
        this.plugin.settings.useUrlInsiders = value;
        this.plugin.saveSettings();
      });
    });
  }
};

// src/main.ts
var svg = `
<path
  fill="currentColor"
  d="M 96.457031 10.777344 L 75.875 0.875 C 73.492188 -0.273438 70.640625 0.210938 68.769531 2.082031 L 29.355469 38.042969 L 12.1875 25.007812 C 10.589844 23.796875 8.355469 23.894531 6.871094 25.246094 L 1.363281 30.253906 C 0.496094 31.042969 0 32.160156 0 33.335938 C -0.00390625 34.507812 0.492188 35.625 1.359375 36.417969 L 16.246094 50 L 1.359375 63.582031 C 0.492188 64.375 -0.00390625 65.492188 0 66.664062 C 0 67.839844 0.496094 68.957031 1.363281 69.746094 L 6.875 74.75 C 8.359375 76.101562 10.59375 76.199219 12.191406 74.988281 L 29.359375 61.953125 L 68.773438 97.914062 C 70.644531 99.785156 73.492188 100.269531 75.875 99.121094 L 96.464844 89.214844 C 98.628906 88.171875 100 85.984375 100 83.582031 L 100 16.414062 C 100 14.011719 98.625 11.820312 96.457031 10.777344 Z M 75.015625 72.699219 L 45.109375 50 L 75.015625 27.300781 Z M 75.015625 72.699219"
/>
`;
(0, import_obsidian2.addIcon)("vscode-logo", svg);
var DEV = false;
var OpenVSCode = class extends import_obsidian2.Plugin {
  constructor() {
    super(...arguments);
    this.refreshIconRibbon = () => {
      var _a;
      (_a = this.ribbonIcon) == null ? void 0 : _a.remove();
      if (this.settings.ribbonIcon) {
        this.ribbonIcon = this.addRibbonIcon("vscode-logo", "VSCode", () => {
          const ribbonCommand = this.settings.ribbonCommandUsesCode ? "openVSCode" : "openVSCodeUrl";
          this[ribbonCommand]();
        });
      }
    };
  }
  async onload() {
    console.log("Loading " + this.manifest.name + " plugin");
    this.addSettingTab(new OpenVSCodeSettingsTab(this.app, this));
    await this.loadSettings();
    this.refreshIconRibbon();
    this.addCommand({
      id: "open-vscode",
      name: "Open as Visual Studio Code workspace",
      callback: this.openVSCode.bind(this)
    });
    this.addCommand({
      id: "open-vscode-via-url",
      name: "Open as Visual Studio Code workspace using a vscode:// URL",
      callback: this.openVSCodeUrl.bind(this)
    });
    DEV = this.app.plugins.enabledPlugins.has("hot-reload") && this.app.plugins.plugins["hot-reload"].enabledPlugins.has(this.manifest.id);
    if (DEV) {
      this.addCommand({
        id: "open-vscode-reload",
        name: "Reload the plugin in dev",
        callback: this.reload.bind(this)
      });
      this.addCommand({
        id: "open-vscode-reset-settings",
        name: "Reset plugins settings to default in dev",
        callback: this.resetSettings.bind(this)
      });
    }
  }
  async openVSCode() {
    var _a;
    if (!(this.app.vault.adapter instanceof import_obsidian2.FileSystemAdapter)) {
      return;
    }
    const { executeTemplate } = this.settings;
    const path = this.app.vault.adapter.getBasePath();
    const file = this.app.workspace.getActiveFile();
    const filePath = (_a = file == null ? void 0 : file.path) != null ? _a : "";
    const { exec } = require("child_process");
    let command = executeTemplate.trim() === "" ? DEFAULT_SETTINGS.executeTemplate : executeTemplate;
    command = replaceAll(command, "{{vaultpath}}", path);
    command = replaceAll(command, "{{filepath}}", filePath);
    if (DEV)
      console.log("[openVSCode]", { command });
    exec(command, (error, stdout, stderr) => {
      if (error) {
        console.error(`[openVSCode] exec error: ${error}`);
      }
    });
  }
  async openVSCodeUrl() {
    var _a;
    if (!(this.app.vault.adapter instanceof import_obsidian2.FileSystemAdapter)) {
      return;
    }
    const { openFile, useUrlInsiders } = this.settings;
    const path = this.app.vault.adapter.getBasePath();
    const file = this.app.workspace.getActiveFile();
    const filePath = (_a = file == null ? void 0 : file.path) != null ? _a : "";
    if (DEV)
      console.log("[open-vscode]", {
        settings: this.settings,
        path,
        filePath
      });
    const protocol = useUrlInsiders ? "vscode-insiders://" : "vscode://";
    let url = `${protocol}file/${path}`;
    if (openFile) {
      url += `/${filePath}`;
      const workspacePath = replaceAll(this.settings.workspacePath, "{{vaultpath}}", path);
      window.open(`vscode://file/${workspacePath}`);
      setTimeout(() => {
        if (DEV)
          console.log("[openVSCode]", { url });
        window.open(url);
      }, 200);
    } else {
      if (DEV)
        console.log("[openVSCode]", { url });
      window.open(url);
    }
  }
  async loadSettings() {
    this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
  }
  async saveSettings() {
    await this.saveData(this.settings);
  }
  async reload() {
    const id = this.manifest.id;
    const plugins = this.app.plugins;
    await plugins.disablePlugin(id);
    await plugins.enablePlugin(id);
    console.log("[open-vscode] reloaded", this);
  }
  async resetSettings() {
    console.log("[open-vscode]", { old: this.settings, DEFAULT_SETTINGS });
    this.settings = DEFAULT_SETTINGS;
    await this.saveData(this.settings);
  }
};
function escapeRegExp(string) {
  return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function replaceAll(str, find, replace) {
  return str.replace(new RegExp(escapeRegExp(find), "g"), replace);
}