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

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

// main.ts
var main_exports = {};
__export(main_exports, {
  default: () => main_default
});
module.exports = __toCommonJS(main_exports);

// src/plugin.ts
var import_obsidian2 = require("obsidian");

// src/constants.ts
var DEFAULT_SETTINGS = {
  commands: {
    Global: false,
    FileExplorer: true,
    TagPane: true,
    Search: false,
    Bookmarks: false
  }
};

// src/provider/base.ts
var ProviderBase = class {
  constructor(plugin) {
    this.plugin = plugin;
    this.registered = false;
  }
  /**
   * Collapse command config
   */
  get collapseCommand() {
    return {
      id: `collapse-${this.leafType}`,
      name: this.collapseCommandName,
      icon: "double-up-arrow-glyph",
      callback: () => {
        this.collapseAll();
      }
    };
  }
  /**
   * Expand command config
   */
  get expandCommand() {
    return {
      id: `expand-${this.leafType}`,
      name: this.expandCommandName,
      icon: "double-down-arrow-glyph",
      callback: () => {
        this.expandAll();
      }
    };
  }
  /**
   * Toggle command config
   */
  get toggleCommand() {
    return {
      id: `toggle-${this.leafType}`,
      name: this.toggleCommandName,
      icon: "double-down-arrow-glyph",
      callback: () => {
        this.toggleCollapse();
      }
    };
  }
  get commands() {
    return [this.collapseCommand, this.expandCommand, this.toggleCommand];
  }
  register() {
    if (this.registered) {
      return;
    }
    for (const command of this.commands) {
      this.plugin.addCommand(command);
    }
    this.registered = true;
  }
  /**
   * Collapse or expand all items for the given leaf
   * @argument collapsed if not provided, will toggle the state
   */
  collapseOrExpandAll(leaf, collapsed) {
    var _a, _b, _c;
    if (collapsed === void 0) {
      if (!((_a = leaf.view.tree) == null ? void 0 : _a.toggleCollapseAll)) {
        console.error(
          `No toggle collapse function found on ${this.leafType} view.`
        );
        return;
      }
      (_b = leaf.view.tree) == null ? void 0 : _b.toggleCollapseAll();
    } else {
      if (!((_c = leaf.view.tree) == null ? void 0 : _c.setCollapseAll)) {
        console.error(`No collapse function found on ${this.leafType} view.`);
        return;
      }
      leaf.view.tree.setCollapseAll(collapsed);
    }
  }
  /**
   * Returns true if every item in the given leaf is collapsed
   */
  allCollapsed(singleLeaf = null) {
    var _a;
    const leaves = singleLeaf ? [singleLeaf] : this.leaves;
    let collapsed = true;
    for (const leaf of leaves) {
      if (((_a = leaf.view.tree) == null ? void 0 : _a.isAllCollapsed) === void 0) {
        console.error("No collapsed state found on view.");
        collapsed = false;
      }
    }
    return collapsed;
  }
  toggleCollapse(singleLeaf = null) {
    const leaves = singleLeaf ? [singleLeaf] : this.leaves;
    for (const leaf of leaves) {
      this.collapseOrExpandAll(leaf);
    }
  }
  /**
   * Collapse all open items in the given leaf or all leaves
   */
  collapseAll(singleLeaf = null) {
    const leaves = singleLeaf ? [singleLeaf] : this.leaves;
    for (const leaf of leaves) {
      this.collapseOrExpandAll(leaf, true);
    }
  }
  /**
   * Expand all collapsed items in the given leaf or all leaves
   */
  expandAll(singleLeaf = null) {
    const leaves = singleLeaf ? [singleLeaf] : this.leaves;
    for (const leaf of leaves) {
      this.collapseOrExpandAll(leaf, false);
    }
  }
  /**
   * Returns all loaded leaves of the class leafType
   */
  get leaves() {
    return this.plugin.app.workspace.getLeavesOfType(this.leafType);
  }
};

// src/provider/file-explorer.ts
var FileExplorerProvider = class extends ProviderBase {
  constructor() {
    super(...arguments);
    this.providerType = "FileExplorer" /* FileExplorer */;
    this.displayName = "File explorer";
    this.leafType = "file-explorer";
    this.collapseCommandName = "Collapse open folders in all file explorers";
    this.expandCommandName = "Expand closed folders in all file explorers";
    this.toggleCommandName = "Toggle collapse in all file explorers";
  }
};

// src/provider/tag-pane.ts
var TagPaneProvider = class extends ProviderBase {
  constructor() {
    super(...arguments);
    this.providerType = "TagPane" /* TagPane */;
    this.displayName = "Tag pane";
    this.leafType = "tag";
    this.collapseCommandName = "Not available";
    this.expandCommandName = "Not available";
    this.toggleCommandName = "Toggle collapse in all tag explorers";
  }
  get commands() {
    return [this.toggleCommand];
  }
  toggleCollapse(singleLeaf) {
    const leaves = singleLeaf ? [singleLeaf] : this.leaves;
    for (const leaf of leaves) {
      if (!leaf.view.collapseOrExpandAllEl) {
        console.error(`No collapse element found on ${this.leafType} view.`);
        return;
      }
      leaf.view.collapseOrExpandAllEl.click();
    }
  }
  collapseAll(_) {
  }
  expandAll(_) {
  }
};

// src/provider/global.ts
var GlobalProvider = class extends ProviderBase {
  constructor() {
    super(...arguments);
    this.providerType = "Global" /* Global */;
    this.displayName = "All supported explorers (global)";
    this.leafType = "";
    this.toggleCommandName = "Toggle collapse state in all supported explorers";
    this.collapseCommandName = "Collapse open items in all supported explorers";
    this.expandCommandName = "Expand closed items in all supported explorers";
  }
  get providers() {
    return this.plugin.allProviders.filter(
      (p) => p.providerType !== "Global" /* Global */
    );
  }
  allCollapsed() {
    return this.providers.every((p) => p.allCollapsed());
  }
  toggleCollapse(_ = null) {
    for (const provider of this.providers) {
      provider.toggleCollapse();
    }
  }
  collapseAll(_ = null) {
    for (const provider of this.providers) {
      provider.collapseAll();
    }
  }
  expandAll(_ = null) {
    for (const provider of this.providers) {
      provider.expandAll();
    }
  }
};

// src/provider/search.ts
var SearchProvider = class extends ProviderBase {
  constructor() {
    super(...arguments);
    this.providerType = "Search" /* Search */;
    this.displayName = "Search";
    this.leafType = "search";
    this.collapseCommandName = "Collapse all in search views";
    this.expandCommandName = "Expand all in search views";
    this.toggleCommandName = "Not available";
  }
  get commands() {
    return [this.collapseCommand, this.expandCommand];
  }
  toggleCollapse() {
  }
  /**
   * Collapse or expand all items for the given leaf
   * @argument collapsed if not provided, will toggle the state
   */
  collapseOrExpandAll(leaf, collapsed) {
    if (collapsed === void 0) {
    } else {
      if (!leaf.view.setCollapseAll) {
        console.error(`No collapse function found on ${this.leafType} view.`);
        return;
      }
      leaf.view.setCollapseAll(collapsed);
    }
  }
};

// src/provider/bookmarks.ts
var BookmarksProvider = class extends ProviderBase {
  constructor() {
    super(...arguments);
    this.providerType = "Bookmarks" /* Bookmarks */;
    this.displayName = "Bookmarks";
    this.leafType = "bookmarks";
    this.toggleCommandName = "Toggle collapse in bookmarks view";
    this.collapseCommandName = "Collapse all in bookmarks view";
    this.expandCommandName = "Expand all in bookmarks view";
  }
};

// src/settings.ts
var import_obsidian = require("obsidian");
var CollapseAllPluginSettings = class extends import_obsidian.PluginSettingTab {
  constructor(app, plugin) {
    super(app, plugin);
    this.plugin = plugin;
  }
  display() {
    this.containerEl.empty();
    this.containerEl.createEl("h3", { text: "Command settings" });
    this.containerEl.createEl("p", {
      text: "Each toggle controls whether commands should be added to collapse and expand that view, or global which operates on all available views."
    });
    this.plugin.allProviders.forEach((provider) => {
      new import_obsidian.Setting(this.containerEl).setName(provider.displayName).addToggle((toggle) => {
        toggle.setTooltip(provider.displayName).setValue(this.plugin.settings.commands[provider.providerType]).onChange(async (value) => {
          this.plugin.settings.commands[provider.providerType] = value;
          if (value === true) {
            provider.register();
          }
          await this.plugin.saveSettings();
        });
      });
    });
  }
};

// src/plugin.ts
var CollapseAllPlugin = class extends import_obsidian2.Plugin {
  constructor() {
    super(...arguments);
    this.settings = DEFAULT_SETTINGS;
    this.providers = {
      ["Global" /* Global */]: new GlobalProvider(this),
      ["FileExplorer" /* FileExplorer */]: new FileExplorerProvider(this),
      ["TagPane" /* TagPane */]: new TagPaneProvider(this),
      ["Search" /* Search */]: new SearchProvider(this),
      ["Bookmarks" /* Bookmarks */]: new BookmarksProvider(this)
    };
  }
  get allProviders() {
    return Object.values(this.providers);
  }
  async onload() {
    this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
    this.allProviders.forEach((provider) => {
      if (this.settings.commands[provider.providerType]) {
        provider.register();
      }
    });
    this.addSettingTab(new CollapseAllPluginSettings(this.app, this));
  }
  saveSettings() {
    return this.saveData(this.settings);
  }
};

// main.ts
var main_default = CollapseAllPlugin;