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

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

// src/setting.ts
var scrollToTopSetting = {
  enabledScrollToTop: true,
  enabledScrollToBottom: true,
  iconScrollToTop: "arrow-up",
  iconScrollToBottom: "arrow-down",
  showTooltip: true,
  scrollTopTooltipText: "Scroll to top",
  scrollBottomTooltipText: "Scroll to bottom"
};

// main.ts
var ROOT_WORKSPACE_CLASS = ".mod-vertical.mod-root";
var ScrollToTopPlugin = class extends import_obsidian.Plugin {
  constructor() {
    super(...arguments);
    this.windowSet = /* @__PURE__ */ new Set();
  }
  addPluginCommand(id, name, callback) {
    this.addCommand({
      id,
      name,
      callback
    });
  }
  scrollToTop() {
    const markdownView = this.app.workspace.getActiveViewOfType(import_obsidian.MarkdownView);
    if (markdownView) {
      const editor = markdownView.editor;
      const preview = markdownView.previewMode;
      editor.scrollTo(0, 0);
      preview && preview.applyScroll(0);
    }
  }
  scrollToBottom() {
    const markdownView = this.app.workspace.getActiveViewOfType(import_obsidian.MarkdownView);
    if (markdownView) {
      const editor = markdownView.editor;
      const preview = markdownView.previewMode;
      editor.exec("goEnd");
      if (preview) {
        let timer = setInterval(() => {
          const prevScroll = preview.getScroll();
          preview.applyScroll(preview.getScroll() + 10);
          if (prevScroll >= preview.getScroll()) {
            clearInterval(timer);
          }
        });
      }
    }
  }
  createScrollElement(config, fn) {
    var _a;
    let topWidget = createEl("div");
    topWidget.setAttribute("class", `div-${config.className}`);
    topWidget.setAttribute("id", config.id);
    let button = new import_obsidian.ButtonComponent(topWidget);
    button.setIcon(config.icon).setClass("buttonItem").onClick(fn);
    if (config.tooltipConfig.showTooltip) {
      button.setTooltip(config.tooltipConfig.tooltipText);
    }
    let curWindow = config.curWindow || window;
    (_a = curWindow.document.body.querySelector(ROOT_WORKSPACE_CLASS)) == null ? void 0 : _a.insertAdjacentElement("afterbegin", topWidget);
    curWindow.document.addEventListener("click", function(event) {
      const activeLeaf = app.workspace.getActiveViewOfType(import_obsidian.MarkdownView);
      if (activeLeaf) {
        topWidget.style.visibility = "visible";
      } else {
        topWidget.style.visibility = "hidden";
      }
    });
  }
  removeButton(id, curWindow) {
    let curWin = curWindow || window;
    const element = curWin.document.getElementById(id);
    if (element) {
      element.remove();
    }
  }
  createButton(window2) {
    const {
      enabledScrollToTop,
      enabledScrollToBottom,
      iconScrollToTop,
      iconScrollToBottom,
      showTooltip,
      scrollTopTooltipText,
      scrollBottomTooltipText
    } = this.settings;
    if (enabledScrollToTop) {
      this.createScrollElement({
        id: "__C_scrollToTop",
        className: "scrollToTop",
        icon: iconScrollToTop,
        curWindow: window2,
        tooltipConfig: {
          showTooltip,
          tooltipText: scrollTopTooltipText
        }
      }, this.scrollToTop.bind(this));
    }
    if (enabledScrollToBottom) {
      this.createScrollElement({
        id: "__C_scrollToBottom",
        className: "scrollToBottom",
        icon: iconScrollToBottom,
        curWindow: window2,
        tooltipConfig: {
          showTooltip,
          tooltipText: scrollBottomTooltipText
        }
      }, this.scrollToBottom.bind(this));
    }
  }
  async onload() {
    await this.loadSettings();
    this.addSettingTab(new ScrollToTopSettingTab(this.app, this));
    this.app.workspace.onLayoutReady(() => {
      this.createButton();
    });
    this.addPluginCommand("scroll-to-top", "Scroll to Top", this.scrollToTop.bind(this));
    this.addPluginCommand("scroll-to-bottom", "Scroll to Bottom", this.scrollToBottom.bind(this));
    this.app.workspace.on("window-open", (win, window2) => {
      this.windowSet.add(window2);
      this.createButton(window2);
    });
    this.app.workspace.on("window-close", (win, window2) => {
      this.windowSet.delete(window2);
    });
    setTimeout(() => {
      this.app.workspace.trigger("css-change");
    }, 300);
  }
  async saveSettings() {
    await this.saveData(this.settings);
  }
  async loadSettings() {
    this.settings = Object.assign({}, scrollToTopSetting, await this.loadData());
  }
  onunload() {
    this.removeButton("__C_scrollToTop");
    this.removeButton("__C_scrollToBottom");
    if (this.windowSet.size > 0) {
      this.windowSet.forEach((window2) => {
        this.removeButton("__C_scrollToTop", window2);
        this.removeButton("__C_scrollToBottom", window2);
      });
    }
  }
};
var ScrollToTopSettingTab = class extends import_obsidian.PluginSettingTab {
  constructor(app2, plugin) {
    super(app2, plugin);
    this.plugin = plugin;
  }
  createSpanWithLinks(text, href, linkText) {
    const span = document.createElement("span");
    span.innerText = text;
    const link = document.createElement("a");
    link.href = href;
    link.innerText = linkText;
    span.appendChild(link);
    return span;
  }
  rebuildButton() {
    this.plugin.removeButton("__C_scrollToTop");
    this.plugin.removeButton("__C_scrollToBottom");
    this.plugin.createButton();
    if (this.plugin.windowSet.size > 0) {
      this.plugin.windowSet.forEach((window2) => {
        this.plugin.removeButton("__C_scrollToTop", window2);
        this.plugin.removeButton("__C_scrollToBottom", window2);
        this.plugin.createButton(window2);
      });
    }
  }
  display() {
    const { containerEl } = this;
    containerEl.empty();
    containerEl.createEl("h2", { text: "Scroll To Top Settings" });
    new import_obsidian.Setting(containerEl).setName("Show scroll to top button").setDesc("Show scroll to top button in the right bottom corner.").addToggle((value) => {
      value.setValue(this.plugin.settings.enabledScrollToTop).onChange(async (value2) => {
        this.plugin.settings.enabledScrollToTop = value2;
        await this.plugin.saveSettings();
        this.rebuildButton();
      });
    });
    new import_obsidian.Setting(containerEl).setName("Show scroll to bottom button").setDesc("Show scroll to bottom button in the right bottom corner.").addToggle((value) => {
      value.setValue(this.plugin.settings.enabledScrollToBottom).onChange(async (value2) => {
        this.plugin.settings.enabledScrollToBottom = value2;
        await this.plugin.saveSettings();
        this.rebuildButton();
      });
    });
    new import_obsidian.Setting(containerEl).setName("Show Tooltip").setDesc("Show tooltip when hover on the button.").addToggle((value) => {
      value.setValue(this.plugin.settings.showTooltip).onChange(async (value2) => {
        this.plugin.settings.showTooltip = value2;
        await this.plugin.saveSettings();
        this.rebuildButton();
      });
    });
    new import_obsidian.Setting(containerEl).setName("tooltip config for top button").setDesc("Change tooltip text of scroll to top button.").addText((value) => {
      value.setValue(this.plugin.settings.scrollTopTooltipText).onChange(async (value2) => {
        this.plugin.settings.scrollTopTooltipText = value2;
        await this.plugin.saveSettings();
        this.rebuildButton();
      });
    });
    new import_obsidian.Setting(containerEl).setName("tooltip config for bottom button").setDesc("Change tooltip text of scroll to bottom button.").addText((value) => {
      value.setValue(this.plugin.settings.scrollBottomTooltipText).onChange(async (value2) => {
        this.plugin.settings.scrollBottomTooltipText = value2;
        await this.plugin.saveSettings();
        this.rebuildButton();
      });
    });
    new import_obsidian.Setting(containerEl).setName("Change icon of scroll to top button").setDesc(this.createSpanWithLinks("Change icon of scroll to top button. You can visit available icons here: ", "https://lucide.dev/", "lucide.dev")).addText((value) => {
      value.setValue(this.plugin.settings.iconScrollToTop).onChange(async (value2) => {
        this.plugin.settings.iconScrollToTop = value2;
        await this.plugin.saveSettings();
        this.rebuildButton();
      });
    });
    new import_obsidian.Setting(containerEl).setName("Change icon of scroll to bottom button").setDesc(this.createSpanWithLinks("Change icon of scroll to bottom button. You can visit available icons here: ", "https://lucide.dev/", "lucide.dev")).addText((value) => {
      value.setValue(this.plugin.settings.iconScrollToBottom).onChange(async (value2) => {
        this.plugin.settings.iconScrollToBottom = value2;
        await this.plugin.saveSettings();
        this.rebuildButton();
      });
    });
  }
};
