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

var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __export = (target, all) => {
  __markAsModule(target);
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __reExport = (target, module2, desc) => {
  if (module2 && typeof module2 === "object" || typeof module2 === "function") {
    for (let key of __getOwnPropNames(module2))
      if (!__hasOwnProp.call(target, key) && key !== "default")
        __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
  }
  return target;
};
var __toModule = (module2) => {
  return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
};
var __async = (__this, __arguments, generator) => {
  return new Promise((resolve, reject) => {
    var fulfilled = (value) => {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    };
    var rejected = (value) => {
      try {
        step(generator.throw(value));
      } catch (e) {
        reject(e);
      }
    };
    var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
    step((generator = generator.apply(__this, __arguments)).next());
  });
};

// main.ts
__export(exports, {
  DEFAULT_SETTINGS: () => DEFAULT_SETTINGS,
  default: () => PackratPlugin
});
var import_obsidian = __toModule(require("obsidian"));
var DEFAULT_SETTINGS = {
  deletion_trigger: "%%done_del%%",
  bottom_trigger: "%%done_end%%",
  archive_trigger: "%%done_log%%",
  archive_filepath: "archive.md"
};
var PackratPlugin = class extends import_obsidian.Plugin {
  onload() {
    return __async(this, null, function* () {
      console.log("Packrat: Loading...");
      yield this.loadSettings();
      this.addSettingTab(new PackratSettingTab(this.app, this));
      this.addCommand({
        id: "run",
        name: "Process completed recurring Tasks within the active note",
        checkCallback: (checking) => {
          const { workspace } = this.app;
          const activeFile = workspace.getActiveFile();
          if (activeFile && activeFile.extension == "md") {
            if (checking) {
              return true;
            }
            this.ProcessCompletedRecurringTasks(activeFile);
          } else {
            return false;
          }
        }
      });
    });
  }
  onunload() {
    console.log("Packrat: Unloading...");
  }
  loadSettings() {
    return __async(this, null, function* () {
      this.settings = Object.assign({}, DEFAULT_SETTINGS, yield this.loadData());
    });
  }
  saveSettings() {
    return __async(this, null, function* () {
      yield this.saveData(this.settings);
    });
  }
  ProcessCompletedRecurringTasks(activeFile) {
    return __async(this, null, function* () {
      try {
        const { vault } = this.app;
        const rruleSignifier = "\u{1F501}".normalize();
        const deleteTrigger = this.settings.deletion_trigger;
        const archiveTrigger = this.settings.archive_trigger;
        const bottomTrigger = this.settings.bottom_trigger;
        const archiveFilename = this.settings.archive_filepath;
        const archiveFile = vault.getAbstractFileByPath(archiveFilename) || (yield vault.create(archiveFilename, ""));
        let deletedTaskCount = 0;
        let movedTaskCount = 0;
        let archivedTaskCount = 0;
        let thisLine = "";
        let writebackLines = [];
        let appendLines = [];
        let archiveLines = [];
        let results = [];
        let fileContents = yield vault.read(activeFile);
        fileContents = fileContents.split("\n");
        for (let i = 0; i < fileContents.length; i++) {
          thisLine = fileContents[i];
          let firstFive = thisLine.substring(0, 5).toUpperCase();
          if (firstFive === "- [X]" && thisLine.indexOf(rruleSignifier) != -1) {
            if (0 < thisLine.indexOf(deleteTrigger)) {
              deletedTaskCount += 1;
              continue;
            }
            if (0 < thisLine.indexOf(archiveTrigger)) {
              archiveLines.push(thisLine);
              archivedTaskCount += 1;
              continue;
            }
            if (0 < thisLine.indexOf(bottomTrigger)) {
              appendLines.push(thisLine);
              movedTaskCount += 1;
              continue;
            }
            writebackLines.push(thisLine);
          } else {
            writebackLines.push(thisLine);
          }
        }
        if (archivedTaskCount > 0) {
          let archiveFileContents = yield vault.read(archiveFile);
          archiveFileContents = archiveFileContents.split("\n");
          archiveFileContents = archiveFileContents.concat(archiveLines);
          vault.modify(archiveFile, archiveFileContents.join("\n"));
        }
        results = writebackLines.concat(appendLines);
        vault.modify(activeFile, results.join("\n"));
        var tdMsg = `${deletedTaskCount} tasks deleted
`;
        var tmMsg = `${movedTaskCount} tasks moved to end of note
`;
        var taMsg = `${archivedTaskCount} tasks archived
`;
        const noticeText = tdMsg + tmMsg + taMsg;
        new import_obsidian.Notice(noticeText);
      } catch (err) {
        new import_obsidian.Notice(err);
        console.log(err);
        return;
      }
    });
  }
};
var PackratSettingTab = class extends import_obsidian.PluginSettingTab {
  constructor(app, plugin) {
    super(app, plugin);
    this.defaultDeletionTrigger = "%%done_del%%";
    this.defaultBottomTrigger = "%%done_move%%";
    this.defaultArchiveTrigger = "%%done_log%%";
    this.defaultArchiveFilepath = "logfile.md";
    this.plugin = plugin;
  }
  display() {
    const { containerEl } = this;
    containerEl.empty();
    containerEl.createEl("h2", { text: "Packrat plugin settings" });
    new import_obsidian.Setting(containerEl).setName("Deletion trigger").setDesc("Text to trigger deletion of completed recurring Task instance").addText((text) => text.setPlaceholder(this.defaultDeletionTrigger).setValue(this.plugin.settings.deletion_trigger).onChange((value) => __async(this, null, function* () {
      console.log("deletion_trigger: " + value);
      this.plugin.settings.deletion_trigger = value;
      yield this.plugin.saveSettings();
    })));
    new import_obsidian.Setting(containerEl).setName('"Move to end of file" trigger').setDesc("Text to trigger moving completed recurring Task instance to bottom of Active note").addText((text) => text.setPlaceholder(this.defaultbottomTrigger).setValue(this.plugin.settings.bottom_trigger).onChange((value) => __async(this, null, function* () {
      console.log("bottom_trigger: " + value);
      this.plugin.settings.bottom_trigger = value;
      yield this.plugin.saveSettings();
    })));
    new import_obsidian.Setting(containerEl).setName("Archive trigger").setDesc("Text to trigger moving completed recurring Task instance to archive note").addText((text) => text.setPlaceholder(this.defaultarchiveTrigger).setValue(this.plugin.settings.archive_trigger).onChange((value) => __async(this, null, function* () {
      console.log("archive_trigger: " + value);
      this.plugin.settings.archive_trigger = value;
      yield this.plugin.saveSettings();
    })));
    new import_obsidian.Setting(containerEl).setName("Archive file").setDesc('Relative filepath to archive file (include ".md" extension)').addText((text) => text.setPlaceholder(this.defaultArchiveFilepath).setValue(this.plugin.settings.archive_filepath).onChange((value) => __async(this, null, function* () {
      console.log("archive_filepath: " + value);
      this.plugin.settings.archive_filepath = value;
      yield this.plugin.saveSettings();
    })));
  }
};
//# sourceMappingURL=data:application/json;base64,[encoded data, 13724 characters]
