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

// src/settings/settings.ts
var import_obsidian4 = require("obsidian");

// src/lib/constant.ts
var SETTINGS_VARIABLES_DATES = "${date}";
var SETTINGS_VARIABLES_NOTEPATH = "${notepath}";
var SETTINGS_VARIABLES_NOTENAME = "${notename}";
var SETTINGS_VARIABLES_NOTEPARENT = "${parent}";
var SETTINGS_VARIABLES_ORIGINALNAME = "${originalname}";
var SETTINGS_VARIABLES_MD5 = "${md5}";
var SETTINGS_ROOT_OBSFOLDER = "obsFolder";
var SETTINGS_ROOT_INFOLDER = "inFolderBelow";
var SETTINGS_ROOT_NEXTTONOTE = "nextToNote";

// src/model/extensionOverride.ts
var import_obsidian3 = require("obsidian");

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

// src/i18n/index.ts
var import_obsidian = require("obsidian");
var currentLanguage = "en";
var translations = {
  "en": {},
  "zh-cn": {}
};
function setLanguage(language) {
  currentLanguage = language;
}
function getCurrentLanguage() {
  return currentLanguage;
}
function registerTranslations(language, translationMap) {
  translations[language] = { ...translations[language], ...translationMap };
}
function t(key, params) {
  const keys = key.split(".");
  let value = translations[currentLanguage];
  for (const k of keys) {
    if (value && typeof value === "object" && k in value) {
      value = value[k];
    } else {
      if (currentLanguage !== "en") {
        let fallbackValue = translations["en"];
        for (const fk of keys) {
          if (fallbackValue && typeof fallbackValue === "object" && fk in fallbackValue) {
            fallbackValue = fallbackValue[fk];
          } else {
            fallbackValue = key;
            break;
          }
        }
        value = fallbackValue;
      } else {
        value = key;
      }
      break;
    }
  }
  let result = typeof value === "string" ? value : key;
  if (params) {
    Object.entries(params).forEach(([paramKey, paramValue]) => {
      result = result.replace(new RegExp(`\\{${paramKey}\\}`, "g"), String(paramValue));
    });
  }
  return result;
}
function detectLanguage() {
  const locale = import_obsidian.moment.locale();
  if (locale.startsWith("zh")) {
    return "zh-cn";
  }
  return "en";
}
function initI18n(language) {
  const initialLanguage = language || detectLanguage();
  setLanguage(initialLanguage);
}

// node_modules/ts-md5/dist/esm/md5.js
var Md5 = class {
  constructor() {
    this._dataLength = 0;
    this._bufferLength = 0;
    this._state = new Int32Array(4);
    this._buffer = new ArrayBuffer(68);
    this._buffer8 = new Uint8Array(this._buffer, 0, 68);
    this._buffer32 = new Uint32Array(this._buffer, 0, 17);
    this.start();
  }
  static hashStr(str, raw = false) {
    return this.onePassHasher.start().appendStr(str).end(raw);
  }
  static hashAsciiStr(str, raw = false) {
    return this.onePassHasher.start().appendAsciiStr(str).end(raw);
  }
  static _hex(x) {
    const hc = Md5.hexChars;
    const ho = Md5.hexOut;
    let n;
    let offset;
    let j;
    let i;
    for (i = 0; i < 4; i += 1) {
      offset = i * 8;
      n = x[i];
      for (j = 0; j < 8; j += 2) {
        ho[offset + 1 + j] = hc.charAt(n & 15);
        n >>>= 4;
        ho[offset + 0 + j] = hc.charAt(n & 15);
        n >>>= 4;
      }
    }
    return ho.join("");
  }
  static _md5cycle(x, k) {
    let a = x[0];
    let b = x[1];
    let c = x[2];
    let d = x[3];
    a += (b & c | ~b & d) + k[0] - 680876936 | 0;
    a = (a << 7 | a >>> 25) + b | 0;
    d += (a & b | ~a & c) + k[1] - 389564586 | 0;
    d = (d << 12 | d >>> 20) + a | 0;
    c += (d & a | ~d & b) + k[2] + 606105819 | 0;
    c = (c << 17 | c >>> 15) + d | 0;
    b += (c & d | ~c & a) + k[3] - 1044525330 | 0;
    b = (b << 22 | b >>> 10) + c | 0;
    a += (b & c | ~b & d) + k[4] - 176418897 | 0;
    a = (a << 7 | a >>> 25) + b | 0;
    d += (a & b | ~a & c) + k[5] + 1200080426 | 0;
    d = (d << 12 | d >>> 20) + a | 0;
    c += (d & a | ~d & b) + k[6] - 1473231341 | 0;
    c = (c << 17 | c >>> 15) + d | 0;
    b += (c & d | ~c & a) + k[7] - 45705983 | 0;
    b = (b << 22 | b >>> 10) + c | 0;
    a += (b & c | ~b & d) + k[8] + 1770035416 | 0;
    a = (a << 7 | a >>> 25) + b | 0;
    d += (a & b | ~a & c) + k[9] - 1958414417 | 0;
    d = (d << 12 | d >>> 20) + a | 0;
    c += (d & a | ~d & b) + k[10] - 42063 | 0;
    c = (c << 17 | c >>> 15) + d | 0;
    b += (c & d | ~c & a) + k[11] - 1990404162 | 0;
    b = (b << 22 | b >>> 10) + c | 0;
    a += (b & c | ~b & d) + k[12] + 1804603682 | 0;
    a = (a << 7 | a >>> 25) + b | 0;
    d += (a & b | ~a & c) + k[13] - 40341101 | 0;
    d = (d << 12 | d >>> 20) + a | 0;
    c += (d & a | ~d & b) + k[14] - 1502002290 | 0;
    c = (c << 17 | c >>> 15) + d | 0;
    b += (c & d | ~c & a) + k[15] + 1236535329 | 0;
    b = (b << 22 | b >>> 10) + c | 0;
    a += (b & d | c & ~d) + k[1] - 165796510 | 0;
    a = (a << 5 | a >>> 27) + b | 0;
    d += (a & c | b & ~c) + k[6] - 1069501632 | 0;
    d = (d << 9 | d >>> 23) + a | 0;
    c += (d & b | a & ~b) + k[11] + 643717713 | 0;
    c = (c << 14 | c >>> 18) + d | 0;
    b += (c & a | d & ~a) + k[0] - 373897302 | 0;
    b = (b << 20 | b >>> 12) + c | 0;
    a += (b & d | c & ~d) + k[5] - 701558691 | 0;
    a = (a << 5 | a >>> 27) + b | 0;
    d += (a & c | b & ~c) + k[10] + 38016083 | 0;
    d = (d << 9 | d >>> 23) + a | 0;
    c += (d & b | a & ~b) + k[15] - 660478335 | 0;
    c = (c << 14 | c >>> 18) + d | 0;
    b += (c & a | d & ~a) + k[4] - 405537848 | 0;
    b = (b << 20 | b >>> 12) + c | 0;
    a += (b & d | c & ~d) + k[9] + 568446438 | 0;
    a = (a << 5 | a >>> 27) + b | 0;
    d += (a & c | b & ~c) + k[14] - 1019803690 | 0;
    d = (d << 9 | d >>> 23) + a | 0;
    c += (d & b | a & ~b) + k[3] - 187363961 | 0;
    c = (c << 14 | c >>> 18) + d | 0;
    b += (c & a | d & ~a) + k[8] + 1163531501 | 0;
    b = (b << 20 | b >>> 12) + c | 0;
    a += (b & d | c & ~d) + k[13] - 1444681467 | 0;
    a = (a << 5 | a >>> 27) + b | 0;
    d += (a & c | b & ~c) + k[2] - 51403784 | 0;
    d = (d << 9 | d >>> 23) + a | 0;
    c += (d & b | a & ~b) + k[7] + 1735328473 | 0;
    c = (c << 14 | c >>> 18) + d | 0;
    b += (c & a | d & ~a) + k[12] - 1926607734 | 0;
    b = (b << 20 | b >>> 12) + c | 0;
    a += (b ^ c ^ d) + k[5] - 378558 | 0;
    a = (a << 4 | a >>> 28) + b | 0;
    d += (a ^ b ^ c) + k[8] - 2022574463 | 0;
    d = (d << 11 | d >>> 21) + a | 0;
    c += (d ^ a ^ b) + k[11] + 1839030562 | 0;
    c = (c << 16 | c >>> 16) + d | 0;
    b += (c ^ d ^ a) + k[14] - 35309556 | 0;
    b = (b << 23 | b >>> 9) + c | 0;
    a += (b ^ c ^ d) + k[1] - 1530992060 | 0;
    a = (a << 4 | a >>> 28) + b | 0;
    d += (a ^ b ^ c) + k[4] + 1272893353 | 0;
    d = (d << 11 | d >>> 21) + a | 0;
    c += (d ^ a ^ b) + k[7] - 155497632 | 0;
    c = (c << 16 | c >>> 16) + d | 0;
    b += (c ^ d ^ a) + k[10] - 1094730640 | 0;
    b = (b << 23 | b >>> 9) + c | 0;
    a += (b ^ c ^ d) + k[13] + 681279174 | 0;
    a = (a << 4 | a >>> 28) + b | 0;
    d += (a ^ b ^ c) + k[0] - 358537222 | 0;
    d = (d << 11 | d >>> 21) + a | 0;
    c += (d ^ a ^ b) + k[3] - 722521979 | 0;
    c = (c << 16 | c >>> 16) + d | 0;
    b += (c ^ d ^ a) + k[6] + 76029189 | 0;
    b = (b << 23 | b >>> 9) + c | 0;
    a += (b ^ c ^ d) + k[9] - 640364487 | 0;
    a = (a << 4 | a >>> 28) + b | 0;
    d += (a ^ b ^ c) + k[12] - 421815835 | 0;
    d = (d << 11 | d >>> 21) + a | 0;
    c += (d ^ a ^ b) + k[15] + 530742520 | 0;
    c = (c << 16 | c >>> 16) + d | 0;
    b += (c ^ d ^ a) + k[2] - 995338651 | 0;
    b = (b << 23 | b >>> 9) + c | 0;
    a += (c ^ (b | ~d)) + k[0] - 198630844 | 0;
    a = (a << 6 | a >>> 26) + b | 0;
    d += (b ^ (a | ~c)) + k[7] + 1126891415 | 0;
    d = (d << 10 | d >>> 22) + a | 0;
    c += (a ^ (d | ~b)) + k[14] - 1416354905 | 0;
    c = (c << 15 | c >>> 17) + d | 0;
    b += (d ^ (c | ~a)) + k[5] - 57434055 | 0;
    b = (b << 21 | b >>> 11) + c | 0;
    a += (c ^ (b | ~d)) + k[12] + 1700485571 | 0;
    a = (a << 6 | a >>> 26) + b | 0;
    d += (b ^ (a | ~c)) + k[3] - 1894986606 | 0;
    d = (d << 10 | d >>> 22) + a | 0;
    c += (a ^ (d | ~b)) + k[10] - 1051523 | 0;
    c = (c << 15 | c >>> 17) + d | 0;
    b += (d ^ (c | ~a)) + k[1] - 2054922799 | 0;
    b = (b << 21 | b >>> 11) + c | 0;
    a += (c ^ (b | ~d)) + k[8] + 1873313359 | 0;
    a = (a << 6 | a >>> 26) + b | 0;
    d += (b ^ (a | ~c)) + k[15] - 30611744 | 0;
    d = (d << 10 | d >>> 22) + a | 0;
    c += (a ^ (d | ~b)) + k[6] - 1560198380 | 0;
    c = (c << 15 | c >>> 17) + d | 0;
    b += (d ^ (c | ~a)) + k[13] + 1309151649 | 0;
    b = (b << 21 | b >>> 11) + c | 0;
    a += (c ^ (b | ~d)) + k[4] - 145523070 | 0;
    a = (a << 6 | a >>> 26) + b | 0;
    d += (b ^ (a | ~c)) + k[11] - 1120210379 | 0;
    d = (d << 10 | d >>> 22) + a | 0;
    c += (a ^ (d | ~b)) + k[2] + 718787259 | 0;
    c = (c << 15 | c >>> 17) + d | 0;
    b += (d ^ (c | ~a)) + k[9] - 343485551 | 0;
    b = (b << 21 | b >>> 11) + c | 0;
    x[0] = a + x[0] | 0;
    x[1] = b + x[1] | 0;
    x[2] = c + x[2] | 0;
    x[3] = d + x[3] | 0;
  }
  /**
   * Initialise buffer to be hashed
   */
  start() {
    this._dataLength = 0;
    this._bufferLength = 0;
    this._state.set(Md5.stateIdentity);
    return this;
  }
  // Char to code point to to array conversion:
  // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt
  // #Example.3A_Fixing_charCodeAt_to_handle_non-Basic-Multilingual-Plane_characters_if_their_presence_earlier_in_the_string_is_unknown
  /**
   * Append a UTF-8 string to the hash buffer
   * @param str String to append
   */
  appendStr(str) {
    const buf8 = this._buffer8;
    const buf32 = this._buffer32;
    let bufLen = this._bufferLength;
    let code;
    let i;
    for (i = 0; i < str.length; i += 1) {
      code = str.charCodeAt(i);
      if (code < 128) {
        buf8[bufLen++] = code;
      } else if (code < 2048) {
        buf8[bufLen++] = (code >>> 6) + 192;
        buf8[bufLen++] = code & 63 | 128;
      } else if (code < 55296 || code > 56319) {
        buf8[bufLen++] = (code >>> 12) + 224;
        buf8[bufLen++] = code >>> 6 & 63 | 128;
        buf8[bufLen++] = code & 63 | 128;
      } else {
        code = (code - 55296) * 1024 + (str.charCodeAt(++i) - 56320) + 65536;
        if (code > 1114111) {
          throw new Error("Unicode standard supports code points up to U+10FFFF");
        }
        buf8[bufLen++] = (code >>> 18) + 240;
        buf8[bufLen++] = code >>> 12 & 63 | 128;
        buf8[bufLen++] = code >>> 6 & 63 | 128;
        buf8[bufLen++] = code & 63 | 128;
      }
      if (bufLen >= 64) {
        this._dataLength += 64;
        Md5._md5cycle(this._state, buf32);
        bufLen -= 64;
        buf32[0] = buf32[16];
      }
    }
    this._bufferLength = bufLen;
    return this;
  }
  /**
   * Append an ASCII string to the hash buffer
   * @param str String to append
   */
  appendAsciiStr(str) {
    const buf8 = this._buffer8;
    const buf32 = this._buffer32;
    let bufLen = this._bufferLength;
    let i;
    let j = 0;
    for (; ; ) {
      i = Math.min(str.length - j, 64 - bufLen);
      while (i--) {
        buf8[bufLen++] = str.charCodeAt(j++);
      }
      if (bufLen < 64) {
        break;
      }
      this._dataLength += 64;
      Md5._md5cycle(this._state, buf32);
      bufLen = 0;
    }
    this._bufferLength = bufLen;
    return this;
  }
  /**
   * Append a byte array to the hash buffer
   * @param input array to append
   */
  appendByteArray(input) {
    const buf8 = this._buffer8;
    const buf32 = this._buffer32;
    let bufLen = this._bufferLength;
    let i;
    let j = 0;
    for (; ; ) {
      i = Math.min(input.length - j, 64 - bufLen);
      while (i--) {
        buf8[bufLen++] = input[j++];
      }
      if (bufLen < 64) {
        break;
      }
      this._dataLength += 64;
      Md5._md5cycle(this._state, buf32);
      bufLen = 0;
    }
    this._bufferLength = bufLen;
    return this;
  }
  /**
   * Get the state of the hash buffer
   */
  getState() {
    const s = this._state;
    return {
      buffer: String.fromCharCode.apply(null, Array.from(this._buffer8)),
      buflen: this._bufferLength,
      length: this._dataLength,
      state: [s[0], s[1], s[2], s[3]]
    };
  }
  /**
   * Override the current state of the hash buffer
   * @param state New hash buffer state
   */
  setState(state) {
    const buf = state.buffer;
    const x = state.state;
    const s = this._state;
    let i;
    this._dataLength = state.length;
    this._bufferLength = state.buflen;
    s[0] = x[0];
    s[1] = x[1];
    s[2] = x[2];
    s[3] = x[3];
    for (i = 0; i < buf.length; i += 1) {
      this._buffer8[i] = buf.charCodeAt(i);
    }
  }
  /**
   * Hash the current state of the hash buffer and return the result
   * @param raw Whether to return the value as an `Int32Array`
   */
  end(raw = false) {
    const bufLen = this._bufferLength;
    const buf8 = this._buffer8;
    const buf32 = this._buffer32;
    const i = (bufLen >> 2) + 1;
    this._dataLength += bufLen;
    const dataBitsLen = this._dataLength * 8;
    buf8[bufLen] = 128;
    buf8[bufLen + 1] = buf8[bufLen + 2] = buf8[bufLen + 3] = 0;
    buf32.set(Md5.buffer32Identity.subarray(i), i);
    if (bufLen > 55) {
      Md5._md5cycle(this._state, buf32);
      buf32.set(Md5.buffer32Identity);
    }
    if (dataBitsLen <= 4294967295) {
      buf32[14] = dataBitsLen;
    } else {
      const matches = dataBitsLen.toString(16).match(/(.*?)(.{0,8})$/);
      if (matches === null) {
        return;
      }
      const lo = parseInt(matches[2], 16);
      const hi = parseInt(matches[1], 16) || 0;
      buf32[14] = lo;
      buf32[15] = hi;
    }
    Md5._md5cycle(this._state, buf32);
    return raw ? this._state : Md5._hex(this._state);
  }
};
Md5.stateIdentity = new Int32Array([1732584193, -271733879, -1732584194, 271733878]);
Md5.buffer32Identity = new Int32Array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
Md5.hexChars = "0123456789abcdef";
Md5.hexOut = [];
Md5.onePassHasher = new Md5();
if (Md5.hashStr("hello") !== "5d41402abc4b2a76b9719d911017c592") {
  throw new Error("Md5 self test failed.");
}

// src/utils.ts
var PASTED_IMAGE_PREFIX = "Pasted image ";
var ImageExtensionRegex = /^(jpe?g|png|gif|svg|bmp|eps|webp)$/i;
function isMarkdownFile(extension) {
  return extension === "md";
}
function isCanvasFile(extension) {
  return extension === "canvas";
}
function isPastedImage(file) {
  if (file instanceof import_obsidian2.TFile) {
    if (file.name.startsWith(PASTED_IMAGE_PREFIX)) {
      return true;
    }
  }
  return false;
}
function isImage(extension) {
  const match = extension.match(ImageExtensionRegex);
  if (match !== null) {
    return true;
  }
  return false;
}
function stripPaths(src, dst) {
  if (src === dst) {
    return { stripedSrc: src, stripedDst: dst };
  }
  const srcParts = src.split("/");
  const dstParts = dst.split("/");
  if (srcParts.length !== dstParts.length) {
    return { stripedSrc: src, stripedDst: dst };
  }
  for (let i = 0; i < srcParts.length; i++) {
    const srcPart = srcParts[i];
    const dstPart = dstParts[i];
    if (srcPart !== dstPart) {
      return {
        stripedSrc: srcParts.slice(0, i + 1).join("/"),
        stripedDst: dstParts.slice(0, i + 1).join("/")
      };
    }
  }
  return { stripedSrc: "", stripedDst: "" };
}
function matchExtension(extension, pattern) {
  if (!pattern || pattern === "")
    return false;
  return new RegExp(pattern).test(extension);
}
function isAttachment(settings, filePath) {
  let file = null;
  if (filePath instanceof import_obsidian2.TAbstractFile) {
    file = filePath;
  } else {
    file = this.app.vault.getAbstractFileByPath(filePath);
  }
  if (file === null || !(file instanceof import_obsidian2.TFile)) {
    return false;
  }
  if (isMarkdownFile(file.extension) || isCanvasFile(file.extension)) {
    return false;
  }
  return !matchExtension(file.extension, settings.excludeExtensionPattern);
}
async function md5sum(adapter, file) {
  const md5 = new Md5();
  if (!adapter.exists(file.path, true)) {
    return "";
  }
  const content = await adapter.readBinary(file.path);
  md5.appendByteArray(new Uint8Array(content));
  const ret = md5.end();
  return ret.toUpperCase();
}
function validateExtensionEntry(setting, plugin) {
  const wrongIndex = [];
  if (setting.extensionOverride !== void 0) {
    const extOverride = setting.extensionOverride;
    if (extOverride.some((ext) => ext.extension === "")) {
      wrongIndex.push({ type: "empty", index: extOverride.findIndex((ext) => ext.extension === "") });
    }
    const duplicate = extOverride.map((ext) => ext.extension).filter((value, index, self) => self.indexOf(value) !== index);
    if (duplicate.length > 0) {
      duplicate.forEach((dupli) => {
        wrongIndex.push({ type: "duplicate", index: extOverride.findIndex((ext) => dupli === ext.extension) });
      });
    }
    const markdown = extOverride.filter((ext) => ext.extension === "md");
    if (markdown.length > 0) {
      wrongIndex.push({ type: "md", index: extOverride.findIndex((ext) => ext.extension === "md") });
    }
    const canvas = extOverride.filter((ext) => ext.extension === "canvas");
    if (canvas.length > 0) {
      wrongIndex.push({ type: "canvas", index: extOverride.findIndex((ext) => ext.extension === "canvas") });
    }
    const excludedFromSettings = plugin.excludeExtensionPattern.split("|");
    const excluded = extOverride.filter((ext) => excludedFromSettings.includes(ext.extension));
    if (excluded.length > 0) {
      wrongIndex.push({
        type: "excluded",
        index: extOverride.findIndex((ext) => excludedFromSettings.includes(ext.extension))
      });
    }
  }
  return wrongIndex;
}
function generateErrorExtensionMessage(type) {
  if (type === "canvas") {
    new import_obsidian2.Notice(t("errors.canvasNotSupported"));
  } else if (type === "md") {
    new import_obsidian2.Notice(t("errors.markdownNotSupported"));
  } else if (type === "empty") {
    new import_obsidian2.Notice(t("errors.extensionEmpty"));
  } else if (type === "duplicate") {
    new import_obsidian2.Notice(t("errors.duplicateExtension"));
  } else if (type === "excluded") {
    new import_obsidian2.Notice(t("errors.excludedExtension"));
  }
}

// src/lib/log.ts
var DEBUG = false;
if (DEBUG)
  console.log("DEBUG is enabled");
function debugLog(...args) {
  if (DEBUG) {
    console.log(new Date().toISOString().slice(11, 23), ...args);
  }
}

// src/model/extensionOverride.ts
function getExtensionOverrideSetting(extension, settings) {
  if (settings.extensionOverride === void 0 || settings.extensionOverride.length === 0) {
    return { extSetting: void 0 };
  }
  for (let i = 0; i < settings.extensionOverride.length; i++) {
    if (matchExtension(extension, settings.extensionOverride[i].extension)) {
      debugLog(
        "getExtensionOverrideSetting - ",
        settings.extensionOverride[i].extension,
        settings.extensionOverride[i]
      );
      return { extSetting: settings.extensionOverride[i] };
    }
  }
  return { extSetting: void 0 };
}
var OverrideExtensionModal = class extends import_obsidian3.Modal {
  constructor(plugin, settings, onSubmit) {
    super(plugin.app);
    this.plugin = plugin;
    this.settings = settings;
    this.onSubmit = onSubmit;
  }
  displaySw(cont) {
    cont.findAll(".setting-item").forEach((el) => {
      var _a;
      if ((_a = el.getAttr("class")) == null ? void 0 : _a.includes("override_root_folder_set")) {
        if (this.settings.saveAttE === "obsFolder") {
          el.hide();
        } else {
          el.show();
        }
      }
    });
  }
  onOpen() {
    const { contentEl } = this;
    contentEl.empty();
    contentEl.createEl("h3", {
      text: t("extensionOverride.title")
    });
    new import_obsidian3.Setting(contentEl).setName(t("extensionOverride.rootPath.name")).setDesc(t("extensionOverride.rootPath.desc")).addDropdown(
      (text) => text.addOption(`${SETTINGS_ROOT_OBSFOLDER}`, t("settings.rootPath.options.obsidian")).addOption(`${SETTINGS_ROOT_INFOLDER}`, t("settings.rootPath.options.inFolder")).addOption(`${SETTINGS_ROOT_NEXTTONOTE}`, t("settings.rootPath.options.nextToNote")).setValue(this.settings.saveAttE).onChange(async (value) => {
        this.settings.saveAttE = value;
        this.displaySw(contentEl);
        this.onOpen();
      })
    );
    if (this.settings.saveAttE !== "obsFolder") {
      new import_obsidian3.Setting(contentEl).setName(t("extensionOverride.rootFolder.name")).setClass("override_root_folder_set").addText(
        (text) => text.setPlaceholder(DEFAULT_SETTINGS.attachPath.attachmentRoot).setValue(this.settings.attachmentRoot).onChange(async (value) => {
          this.settings.attachmentRoot = value;
        })
      );
    }
    new import_obsidian3.Setting(contentEl).setName(t("extensionOverride.attachmentPath.name")).setDesc(t("extensionOverride.attachmentPath.desc")).addText(
      (text) => text.setPlaceholder(DEFAULT_SETTINGS.attachPath.attachmentPath).setValue(this.settings.attachmentPath).onChange(async (value) => {
        this.settings.attachmentPath = value;
      })
    );
    new import_obsidian3.Setting(contentEl).setName(t("extensionOverride.attachmentFormat.name")).setDesc(t("extensionOverride.attachmentFormat.desc")).addText(
      (text) => text.setPlaceholder(DEFAULT_SETTINGS.attachPath.attachFormat).setValue(this.settings.attachFormat).onChange(async (value) => {
        this.settings.attachFormat = value;
      })
    );
    new import_obsidian3.Setting(contentEl).addButton(
      (button) => button.setButtonText(t("extensionOverride.buttons.save")).onClick(async () => {
        this.onSubmit(this.settings);
        this.close();
      })
    );
  }
  onClose() {
    const { contentEl } = this;
    contentEl.empty();
  }
};

// src/i18n/locales/en.ts
var en = {
  // 通用
  common: {
    save: "Save",
    cancel: "Cancel",
    delete: "Delete",
    edit: "Edit",
    add: "Add",
    remove: "Remove",
    confirm: "Confirm",
    close: "Close"
  },
  // 设置页面
  settings: {
    title: "Attachment Management Settings",
    language: {
      name: "Language",
      desc: "Select the interface language"
    },
    rootPath: {
      name: "Root path to save attachment",
      desc: "Select root path of attachment",
      options: {
        obsidian: "Copy Obsidian settings",
        inFolder: "In the folder specified below",
        nextToNote: "Next to note in folder specified below"
      }
    },
    rootFolder: {
      name: "Root folder",
      desc: "Root folder of new attachment"
    },
    attachmentPath: {
      name: "Attachment path",
      desc: "Path of attachment in root folder, available variables {{notepath}}, {{notename}}, {{parent}}"
    },
    attachmentFormat: {
      name: "Attachment format",
      desc: "Define how to name the attachment file, available variables {{dates}}, {{notename}}, {{md5}} and {{originalname}}."
    },
    dateFormat: {
      name: "Date format",
      desc: "Moment date format to use",
      linkText: "Moment format options"
    },
    autoRename: {
      name: "Automatically rename attachment",
      desc: "Automatically rename the attachment folder/filename when you rename the folder/filename where the corresponding md/canvas file be placed."
    },
    extensionOverride: {
      name: "Extension override",
      desc: "Using the extension override if you want to autorename the attachment with a specific extension (e.g. pdf or zip).",
      addButton: "Add extension overrides",
      extension: {
        name: "Extension",
        desc: "Extension to override",
        placeholder: "pdf|docx?"
      },
      tooltips: {
        remove: "Remove extension override",
        edit: "Edit extension override",
        save: "Save extension override"
      },
      saved: "Saved extension override"
    },
    excludeExtension: {
      name: "Exclude extension pattern",
      desc: "Regex pattern to exclude certain extensions from being handled.",
      placeholder: "pdf|docx?|xlsx?|pptx?|zip|rar"
    },
    excludedPaths: {
      name: "Excluded paths",
      desc: "Provide the full path of the folder names (case sensitive and without leading slash '/') divided by semicolon (;) to be excluded from renaming."
    },
    excludeSubpaths: {
      name: "Exclude subpaths",
      desc: "Turn on this option if you want to also exclude all subfolders of the folder paths provided above."
    }
  },
  // 覆盖设置模态框
  override: {
    title: "Overriding Settings",
    menuTitle: "Overriding attachment setting",
    addExtensionOverrides: "Add extension overrides",
    extension: {
      name: "Extension",
      desc: "Extension to override",
      placeholder: "pdf"
    },
    buttons: {
      reset: "Reset",
      submit: "Submit"
    },
    notifications: {
      reset: "Reset attachment setting of {path}",
      overridden: "Overridden attachment setting of {path}"
    }
  },
  // 扩展覆盖模态框
  extensionOverride: {
    title: "Extension Override Settings",
    extension: {
      name: "Extension",
      desc: "Extension pattern to override (e.g., pdf, docx, jpg)",
      placeholder: "pdf|docx?"
    },
    rootPath: {
      name: "Root path to save attachment",
      desc: "Select root path of attachment for this extension"
    },
    rootFolder: {
      name: "Root folder",
      desc: "Root folder for this extension"
    },
    attachmentPath: {
      name: "Attachment path",
      desc: "Path of attachment in root folder for this extension"
    },
    attachmentFormat: {
      name: "Attachment format",
      desc: "Define how to name the attachment file for this extension"
    },
    buttons: {
      save: "Save"
    },
    notice: {
      extensionEmpty: "Extension cannot be empty",
      extensionExists: "Extension already exists",
      saved: "Extension override saved successfully"
    }
  },
  // 确认对话框
  confirm: {
    title: "Tips",
    message: "This operation is irreversible and experimental. Please backup your vault first!",
    continue: "Continue",
    deleteOverride: "Are you sure you want to delete this override setting?",
    deleteExtensionOverride: "Are you sure you want to delete this extension override?"
  },
  // 通知消息
  notices: {
    settingsSaved: "Settings saved successfully",
    overrideSaved: "Override setting saved successfully",
    overrideDeleted: "Override setting deleted successfully",
    extensionOverrideSaved: "Extension override saved successfully",
    extensionOverrideDeleted: "Extension override deleted successfully",
    attachmentRenamed: "Attachment renamed successfully",
    attachmentMoved: "Attachment moved successfully",
    arrangeCompleted: "Arrange completed",
    fileExcluded: "{path} was excluded",
    resetAttachmentSetting: "Reset attachment setting of {path}",
    error: {
      invalidPath: "Invalid path specified",
      fileNotFound: "File not found",
      permissionDenied: "Permission denied",
      unknownError: "An unknown error occurred"
    }
  },
  // 命令
  commands: {
    rearrangeActiveFile: "Rearrange attachments for active file",
    rearrangeAllFiles: "Rearrange attachments for all files",
    openSettings: "Open Attachment Management settings",
    overrideAttachmentSetting: "Override attachment setting",
    rearrangeAllLinks: "Rearrange all linked attachments",
    rearrangeActiveLinks: "Rearrange linked attachments",
    resetOverrideSetting: "Reset override setting",
    clearUnusedStorage: "Clear unused original name storage"
  },
  // 错误消息
  errors: {
    canvasNotSupported: "Canvas is not supported as an extension override.",
    markdownNotSupported: "Markdown is not supported as an extension override.",
    extensionEmpty: "Extension override cannot be empty.",
    duplicateExtension: "Duplicate extension override.",
    excludedExtension: "Extension override cannot be an excluded extension."
  }
};

// src/i18n/locales/zh-cn.ts
var zhCn = {
  // 通用
  common: {
    save: "\u4FDD\u5B58",
    cancel: "\u53D6\u6D88",
    delete: "\u5220\u9664",
    edit: "\u7F16\u8F91",
    add: "\u6DFB\u52A0",
    remove: "\u79FB\u9664",
    confirm: "\u786E\u8BA4",
    close: "\u5173\u95ED"
  },
  // 设置页面
  settings: {
    title: "\u9644\u4EF6\u7BA1\u7406\u8BBE\u7F6E",
    language: {
      name: "\u8BED\u8A00",
      desc: "\u9009\u62E9\u754C\u9762\u8BED\u8A00"
    },
    rootPath: {
      name: "\u9644\u4EF6\u4FDD\u5B58\u6839\u8DEF\u5F84",
      desc: "\u9009\u62E9\u9644\u4EF6\u7684\u6839\u8DEF\u5F84",
      options: {
        obsidian: "\u590D\u5236 Obsidian \u8BBE\u7F6E",
        inFolder: "\u5728\u4E0B\u65B9\u6307\u5B9A\u7684\u6587\u4EF6\u5939\u4E2D",
        nextToNote: "\u5728\u7B14\u8BB0\u65C1\u8FB9\u7684\u6307\u5B9A\u6587\u4EF6\u5939\u4E2D"
      }
    },
    rootFolder: {
      name: "\u6839\u6587\u4EF6\u5939",
      desc: "\u65B0\u9644\u4EF6\u7684\u6839\u6587\u4EF6\u5939"
    },
    attachmentPath: {
      name: "\u9644\u4EF6\u8DEF\u5F84",
      desc: "\u9644\u4EF6\u5728\u6839\u6587\u4EF6\u5939\u4E2D\u7684\u8DEF\u5F84\uFF0C\u53EF\u7528\u53D8\u91CF {{notepath}}\u3001{{notename}}\u3001{{parent}}"
    },
    attachmentFormat: {
      name: "\u9644\u4EF6\u683C\u5F0F",
      desc: "\u5B9A\u4E49\u5982\u4F55\u547D\u540D\u9644\u4EF6\u6587\u4EF6\uFF0C\u53EF\u7528\u53D8\u91CF {{dates}}\u3001{{notename}}\u3001{{md5}} \u548C {{originalname}}\u3002"
    },
    dateFormat: {
      name: "\u65E5\u671F\u683C\u5F0F",
      desc: "\u4F7F\u7528\u7684 Moment \u65E5\u671F\u683C\u5F0F",
      linkText: "Moment \u683C\u5F0F\u9009\u9879"
    },
    autoRename: {
      name: "\u81EA\u52A8\u91CD\u547D\u540D\u9644\u4EF6",
      desc: "\u5F53\u60A8\u91CD\u547D\u540D\u5BF9\u5E94 md/canvas \u6587\u4EF6\u6240\u5728\u7684\u6587\u4EF6\u5939/\u6587\u4EF6\u540D\u65F6\uFF0C\u81EA\u52A8\u91CD\u547D\u540D\u9644\u4EF6\u6587\u4EF6\u5939/\u6587\u4EF6\u540D\u3002"
    },
    extensionOverride: {
      name: "\u6269\u5C55\u540D\u8986\u76D6",
      desc: "\u5982\u679C\u60A8\u60F3\u8981\u5BF9\u7279\u5B9A\u6269\u5C55\u540D\u7684\u9644\u4EF6\u8FDB\u884C\u81EA\u52A8\u91CD\u547D\u540D\uFF08\u4F8B\u5982 pdf \u6216 zip\uFF09\uFF0C\u8BF7\u4F7F\u7528\u6269\u5C55\u540D\u8986\u76D6\u3002",
      addButton: "\u6DFB\u52A0\u6269\u5C55\u540D\u8986\u76D6",
      extension: {
        name: "\u6269\u5C55\u540D",
        desc: "\u8981\u8986\u76D6\u7684\u6269\u5C55\u540D",
        placeholder: "pdf|docx?"
      },
      tooltips: {
        remove: "\u79FB\u9664\u6269\u5C55\u540D\u8986\u76D6",
        edit: "\u7F16\u8F91\u6269\u5C55\u540D\u8986\u76D6",
        save: "\u4FDD\u5B58\u6269\u5C55\u540D\u8986\u76D6"
      },
      saved: "\u5DF2\u4FDD\u5B58\u6269\u5C55\u540D\u8986\u76D6"
    },
    excludeExtension: {
      name: "\u6392\u9664\u6269\u5C55\u540D\u6A21\u5F0F",
      desc: "\u7528\u4E8E\u6392\u9664\u67D0\u4E9B\u6269\u5C55\u540D\u4E0D\u88AB\u5904\u7406\u7684\u6B63\u5219\u8868\u8FBE\u5F0F\u6A21\u5F0F\u3002",
      placeholder: "pdf|docx?|xlsx?|pptx?|zip|rar"
    },
    excludedPaths: {
      name: "\u6392\u9664\u8DEF\u5F84",
      desc: '\u63D0\u4F9B\u8981\u4ECE\u91CD\u547D\u540D\u4E2D\u6392\u9664\u7684\u6587\u4EF6\u5939\u540D\u79F0\u7684\u5B8C\u6574\u8DEF\u5F84\uFF08\u533A\u5206\u5927\u5C0F\u5199\u4E14\u4E0D\u5E26\u524D\u5BFC\u659C\u6760 "/"\uFF09\uFF0C\u7528\u5206\u53F7\uFF08;\uFF09\u5206\u9694\u3002'
    },
    excludeSubpaths: {
      name: "\u6392\u9664\u5B50\u8DEF\u5F84",
      desc: "\u5982\u679C\u60A8\u8FD8\u60F3\u6392\u9664\u4E0A\u9762\u63D0\u4F9B\u7684\u6587\u4EF6\u5939\u8DEF\u5F84\u7684\u6240\u6709\u5B50\u6587\u4EF6\u5939\uFF0C\u8BF7\u6253\u5F00\u6B64\u9009\u9879\u3002"
    }
  },
  // 覆盖设置模态框
  override: {
    title: "\u8986\u76D6\u8BBE\u7F6E",
    menuTitle: "\u8986\u76D6\u9644\u4EF6\u8BBE\u7F6E",
    addExtensionOverrides: "\u6DFB\u52A0\u6269\u5C55\u540D\u8986\u76D6",
    extension: {
      name: "\u6269\u5C55\u540D",
      desc: "\u8981\u8986\u76D6\u7684\u6269\u5C55\u540D",
      placeholder: "pdf"
    },
    buttons: {
      reset: "\u91CD\u7F6E",
      submit: "\u63D0\u4EA4"
    },
    notifications: {
      reset: "\u5DF2\u91CD\u7F6E {path} \u7684\u9644\u4EF6\u8BBE\u7F6E",
      overridden: "\u5DF2\u8986\u76D6 {path} \u7684\u9644\u4EF6\u8BBE\u7F6E"
    }
  },
  // 扩展覆盖模态框
  extensionOverride: {
    title: "\u6269\u5C55\u540D\u8986\u76D6\u8BBE\u7F6E",
    extension: {
      name: "\u6269\u5C55\u540D",
      desc: "\u8981\u8986\u76D6\u7684\u6269\u5C55\u540D\u6A21\u5F0F\uFF08\u4F8B\u5982\uFF1Apdf\u3001docx\u3001jpg\uFF09",
      placeholder: "pdf|docx?"
    },
    rootPath: {
      name: "\u9644\u4EF6\u4FDD\u5B58\u6839\u8DEF\u5F84",
      desc: "\u9009\u62E9\u6B64\u6269\u5C55\u540D\u7684\u9644\u4EF6\u6839\u8DEF\u5F84"
    },
    rootFolder: {
      name: "\u6839\u6587\u4EF6\u5939",
      desc: "\u6B64\u6269\u5C55\u540D\u7684\u6839\u6587\u4EF6\u5939"
    },
    attachmentPath: {
      name: "\u9644\u4EF6\u8DEF\u5F84",
      desc: "\u6B64\u6269\u5C55\u540D\u5728\u6839\u6587\u4EF6\u5939\u4E2D\u7684\u9644\u4EF6\u8DEF\u5F84"
    },
    attachmentFormat: {
      name: "\u9644\u4EF6\u683C\u5F0F",
      desc: "\u5B9A\u4E49\u6B64\u6269\u5C55\u540D\u7684\u9644\u4EF6\u6587\u4EF6\u547D\u540D\u65B9\u5F0F"
    },
    buttons: {
      save: "\u4FDD\u5B58"
    },
    notice: {
      extensionEmpty: "\u6269\u5C55\u540D\u4E0D\u80FD\u4E3A\u7A7A",
      extensionExists: "\u6269\u5C55\u540D\u5DF2\u5B58\u5728",
      saved: "\u6269\u5C55\u540D\u8986\u76D6\u4FDD\u5B58\u6210\u529F"
    }
  },
  // 确认对话框
  confirm: {
    title: "\u63D0\u793A",
    message: "\u6B64\u64CD\u4F5C\u4E0D\u53EF\u9006\u4E14\u4E3A\u5B9E\u9A8C\u6027\u529F\u80FD\uFF0C\u8BF7\u5148\u5907\u4EFD\u60A8\u7684\u5E93\uFF01",
    continue: "\u7EE7\u7EED",
    deleteOverride: "\u60A8\u786E\u5B9A\u8981\u5220\u9664\u6B64\u8986\u76D6\u8BBE\u7F6E\u5417\uFF1F",
    deleteExtensionOverride: "\u60A8\u786E\u5B9A\u8981\u5220\u9664\u6B64\u6269\u5C55\u540D\u8986\u76D6\u5417\uFF1F"
  },
  // 通知消息
  notices: {
    settingsSaved: "\u8BBE\u7F6E\u4FDD\u5B58\u6210\u529F",
    overrideSaved: "\u8986\u76D6\u8BBE\u7F6E\u4FDD\u5B58\u6210\u529F",
    overrideDeleted: "\u8986\u76D6\u8BBE\u7F6E\u5220\u9664\u6210\u529F",
    extensionOverrideSaved: "\u6269\u5C55\u540D\u8986\u76D6\u4FDD\u5B58\u6210\u529F",
    extensionOverrideDeleted: "\u6269\u5C55\u540D\u8986\u76D6\u5220\u9664\u6210\u529F",
    attachmentRenamed: "\u9644\u4EF6\u91CD\u547D\u540D\u6210\u529F",
    attachmentMoved: "\u9644\u4EF6\u79FB\u52A8\u6210\u529F",
    error: {
      invalidPath: "\u6307\u5B9A\u7684\u8DEF\u5F84\u65E0\u6548",
      fileNotFound: "\u6587\u4EF6\u672A\u627E\u5230",
      permissionDenied: "\u6743\u9650\u88AB\u62D2\u7EDD",
      unknownError: "\u53D1\u751F\u672A\u77E5\u9519\u8BEF"
    }
  },
  // 命令
  commands: {
    rearrangeActiveFile: "\u91CD\u65B0\u6574\u7406\u5F53\u524D\u6587\u4EF6\u7684\u9644\u4EF6",
    rearrangeAllFiles: "\u91CD\u65B0\u6574\u7406\u6240\u6709\u6587\u4EF6\u7684\u9644\u4EF6",
    openSettings: "\u6253\u5F00\u9644\u4EF6\u7BA1\u7406\u8BBE\u7F6E",
    overrideAttachmentSetting: "\u8986\u76D6\u9644\u4EF6\u8BBE\u7F6E",
    rearrangeAllLinks: "\u91CD\u65B0\u6574\u7406\u6240\u6709\u94FE\u63A5\u7684\u9644\u4EF6",
    rearrangeActiveLinks: "\u91CD\u65B0\u6574\u7406\u94FE\u63A5\u7684\u9644\u4EF6",
    resetOverrideSetting: "\u91CD\u7F6E\u8986\u76D6\u8BBE\u7F6E",
    clearUnusedStorage: "\u6E05\u7406\u672A\u4F7F\u7528\u7684\u539F\u59CB\u540D\u79F0\u5B58\u50A8"
  },
  // 通知消息
  notifications: {
    success: "\u64CD\u4F5C\u6210\u529F\u5B8C\u6210",
    error: "\u53D1\u751F\u9519\u8BEF",
    warning: "\u8B66\u544A",
    arrangeCompleted: "\u6574\u7406\u5B8C\u6210",
    fileExcluded: "{path} \u5DF2\u88AB\u6392\u9664",
    resetAttachmentSetting: "\u5DF2\u91CD\u7F6E {path} \u7684\u9644\u4EF6\u8BBE\u7F6E"
  },
  // 错误消息
  errors: {
    canvasNotSupported: "\u4E0D\u652F\u6301\u5C06 Canvas \u4F5C\u4E3A\u6269\u5C55\u8986\u76D6\u3002",
    markdownNotSupported: "\u4E0D\u652F\u6301\u5C06 Markdown \u4F5C\u4E3A\u6269\u5C55\u8986\u76D6\u3002",
    extensionEmpty: "\u6269\u5C55\u8986\u76D6\u4E0D\u80FD\u4E3A\u7A7A\u3002",
    duplicateExtension: "\u91CD\u590D\u7684\u6269\u5C55\u8986\u76D6\u3002",
    excludedExtension: "\u6269\u5C55\u8986\u76D6\u4E0D\u80FD\u662F\u88AB\u6392\u9664\u7684\u6269\u5C55\u3002"
  }
};

// src/i18n/loader.ts
function loadAllTranslations() {
  registerTranslations("en", en);
  registerTranslations("zh-cn", zhCn);
}
function getSupportedLanguages() {
  return [
    {
      code: "en",
      name: "English",
      nativeName: "English"
    },
    {
      code: "zh-cn",
      name: "Chinese (Simplified)",
      nativeName: "\u7B80\u4F53\u4E2D\u6587"
    }
  ];
}

// src/settings/settings.ts
var DEFAULT_SETTINGS = {
  language: "en",
  attachPath: {
    attachmentRoot: "",
    saveAttE: `${SETTINGS_ROOT_OBSFOLDER}`,
    attachmentPath: `${SETTINGS_VARIABLES_NOTEPATH}/${SETTINGS_VARIABLES_NOTENAME}`,
    attachFormat: `IMG-${SETTINGS_VARIABLES_DATES}`,
    type: "GLOBAL" /* GLOBAL */
  },
  dateFormat: "YYYYMMDDHHmmssSSS",
  excludeExtensionPattern: "",
  autoRenameAttachment: true,
  excludedPaths: "",
  excludePathsArray: [],
  excludeSubpaths: false,
  originalNameStorage: [],
  overridePath: {},
  disableNotification: false
};
var AttachmentManagementSettingTab = class extends import_obsidian4.PluginSettingTab {
  constructor(app2, plugin) {
    super(app2, plugin);
    this.plugin = plugin;
  }
  displaySw(cont) {
    cont.findAll(".setting-item").forEach((el) => {
      var _a;
      if ((_a = el.getAttr("class")) == null ? void 0 : _a.includes("root_folder_set")) {
        if (this.plugin.settings.attachPath.saveAttE === "obsFolder") {
          el.hide();
        } else {
          el.show();
        }
      }
    });
  }
  splitPath(path2) {
    const splitted = path2.split(";");
    const rets = [];
    for (const s of splitted) {
      rets.push(s.trim());
    }
    return { splittedPaths: rets };
  }
  display() {
    const { containerEl } = this;
    containerEl.empty();
    containerEl.createEl("h2", { text: t("settings.title") });
    new import_obsidian4.Setting(containerEl).setName(t("settings.language.name")).setDesc(t("settings.language.desc")).addDropdown((dropdown) => {
      const languages = getSupportedLanguages();
      languages.forEach((lang) => {
        dropdown.addOption(lang.code, lang.nativeName);
      });
      dropdown.setValue(getCurrentLanguage());
      dropdown.onChange(async (value) => {
        setLanguage(value);
        this.plugin.settings.language = value;
        await this.plugin.saveSettings();
        this.display();
      });
    });
    new import_obsidian4.Setting(containerEl).setName(t("settings.rootPath.name")).setDesc(t("settings.rootPath.desc")).addDropdown(
      (text) => text.addOption(`${SETTINGS_ROOT_OBSFOLDER}`, t("settings.rootPath.options.obsidian")).addOption(`${SETTINGS_ROOT_INFOLDER}`, t("settings.rootPath.options.inFolder")).addOption(`${SETTINGS_ROOT_NEXTTONOTE}`, t("settings.rootPath.options.nextToNote")).setValue(this.plugin.settings.attachPath.saveAttE).onChange(async (value) => {
        this.plugin.settings.attachPath.saveAttE = value;
        this.displaySw(containerEl);
        await this.plugin.saveSettings();
      })
    );
    new import_obsidian4.Setting(containerEl).setName(t("settings.rootFolder.name")).setDesc(t("settings.rootFolder.desc")).setClass("root_folder_set").addText(
      (text) => text.setPlaceholder(DEFAULT_SETTINGS.attachPath.attachmentRoot).setValue(this.plugin.settings.attachPath.attachmentRoot).onChange(async (value) => {
        debugLog("setting - attachment root:" + value);
        this.plugin.settings.attachPath.attachmentRoot = value;
        await this.plugin.saveSettings();
      })
    );
    new import_obsidian4.Setting(containerEl).setName(t("settings.attachmentPath.name")).setDesc(t("settings.attachmentPath.desc")).addText(
      (text) => text.setPlaceholder(DEFAULT_SETTINGS.attachPath.attachmentPath).setValue(this.plugin.settings.attachPath.attachmentPath).onChange(async (value) => {
        debugLog("setting - attachment path:" + value);
        this.plugin.settings.attachPath.attachmentPath = value;
        await this.plugin.saveSettings();
      })
    );
    new import_obsidian4.Setting(containerEl).setName(t("settings.attachmentFormat.name")).setDesc(t("settings.attachmentFormat.desc")).addText(
      (text) => text.setPlaceholder(DEFAULT_SETTINGS.attachPath.attachFormat).setValue(this.plugin.settings.attachPath.attachFormat).onChange(async (value) => {
        debugLog("setting - attachment format:" + value);
        this.plugin.settings.attachPath.attachFormat = value;
        await this.plugin.saveSettings();
      })
    );
    new import_obsidian4.Setting(containerEl).setName(t("settings.dateFormat.name")).setDesc(
      createFragment((frag) => {
        frag.appendText(t("settings.dateFormat.desc") + " ");
        frag.createEl("a", {
          href: "https://momentjscom.readthedocs.io/en/latest/moment/04-displaying/01-format",
          text: t("settings.dateFormat.linkText")
        });
      })
    ).addMomentFormat((component) => {
      component.setPlaceholder(DEFAULT_SETTINGS.dateFormat).setValue(this.plugin.settings.dateFormat).onChange(async (value) => {
        debugLog("setting - date format:" + value);
        this.plugin.settings.dateFormat = value;
        await this.plugin.saveSettings();
      });
    });
    new import_obsidian4.Setting(containerEl).setName(t("settings.autoRename.name")).setDesc(t("settings.autoRename.desc")).addToggle(
      (toggle) => toggle.setValue(this.plugin.settings.autoRenameAttachment).onChange(async (value) => {
        debugLog("setting - automatically rename attachment folder:" + value);
        this.plugin.settings.autoRenameAttachment = value;
        await this.plugin.saveSettings();
      })
    );
    new import_obsidian4.Setting(containerEl).setName(t("settings.extensionOverride.name")).setDesc(t("settings.extensionOverride.desc")).addButton((btn) => {
      btn.setButtonText(t("settings.extensionOverride.addButton")).onClick(async () => {
        if (this.plugin.settings.attachPath.extensionOverride === void 0) {
          this.plugin.settings.attachPath.extensionOverride = [];
        }
        this.plugin.settings.attachPath.extensionOverride.push({
          extension: "",
          attachmentRoot: this.plugin.settings.attachPath.attachmentRoot,
          saveAttE: this.plugin.settings.attachPath.saveAttE,
          attachmentPath: this.plugin.settings.attachPath.attachmentPath,
          attachFormat: this.plugin.settings.attachPath.attachFormat
        });
        await this.plugin.saveSettings();
        this.display();
      });
    });
    if (this.plugin.settings.attachPath.extensionOverride !== void 0) {
      this.plugin.settings.attachPath.extensionOverride.forEach((ext) => {
        new import_obsidian4.Setting(containerEl).setName(t("settings.extensionOverride.extension.name")).setDesc(t("settings.extensionOverride.extension.desc")).setClass("override_extension_set").addText(
          (text) => text.setPlaceholder(t("settings.extensionOverride.extension.placeholder")).setValue(ext.extension).onChange(async (value) => {
            ext.extension = value;
          })
        ).addButton((btn) => {
          btn.setIcon("trash").setTooltip(t("settings.extensionOverride.tooltips.remove")).onClick(async () => {
            var _a, _b, _c;
            const index = (_b = (_a = this.plugin.settings.attachPath.extensionOverride) == null ? void 0 : _a.indexOf(ext)) != null ? _b : -1;
            (_c = this.plugin.settings.attachPath.extensionOverride) == null ? void 0 : _c.splice(index, 1);
            await this.plugin.saveSettings();
            this.display();
          });
        }).addButton((btn) => {
          btn.setIcon("pencil").setTooltip(t("settings.extensionOverride.tooltips.edit")).onClick(async () => {
            new OverrideExtensionModal(this.plugin, ext, (result) => {
              ext = result;
            }).open();
          });
        }).addButton((btn) => {
          btn.setIcon("check").setTooltip(t("settings.extensionOverride.tooltips.save")).onClick(async () => {
            const wrongIndex = validateExtensionEntry(this.plugin.settings.attachPath, this.plugin.settings);
            if (wrongIndex.length > 0) {
              for (const i of wrongIndex) {
                const resIndex = i.index < 0 ? 0 : i.index;
                const wrongSetting = containerEl.getElementsByClassName("override_extension_set")[resIndex];
                wrongSetting.getElementsByTagName("input")[0].style.border = "1px solid var(--color-red)";
                generateErrorExtensionMessage(i.type);
              }
              return;
            }
            await this.plugin.saveSettings();
            this.display();
            new import_obsidian4.Notice(t("settings.extensionOverride.saveNotice"));
          });
        });
      });
    }
    new import_obsidian4.Setting(containerEl).setName(t("settings.excludeExtension.name")).setDesc(t("settings.excludeExtension.desc")).addText(
      (text) => text.setPlaceholder(t("settings.excludeExtension.placeholder")).setValue(this.plugin.settings.excludeExtensionPattern).onChange(async (value) => {
        this.plugin.settings.excludeExtensionPattern = value;
        await this.plugin.saveSettings();
      })
    );
    new import_obsidian4.Setting(containerEl).setName(t("settings.excludedPaths.name")).setDesc(t("settings.excludedPaths.desc")).addTextArea((component) => {
      component.setValue(this.plugin.settings.excludedPaths).onChange(async (value) => {
        this.plugin.settings.excludedPaths = value;
        const { splittedPaths } = this.splitPath(value);
        this.plugin.settings.excludePathsArray = splittedPaths;
        debugLog("setting - excluded paths:" + value, splittedPaths);
        await this.plugin.saveSettings();
      });
    });
    new import_obsidian4.Setting(containerEl).setName(t("settings.excludeSubpaths.name")).setDesc(t("settings.excludeSubpaths.desc")).addToggle(
      (toggle) => toggle.setValue(this.plugin.settings.excludeSubpaths).onChange(async (value) => {
        debugLog("setting - excluded subpaths:" + value);
        this.plugin.settings.excludeSubpaths = value;
        await this.plugin.saveSettings();
      })
    );
    this.displaySw(containerEl);
  }
};

// src/model/override.ts
var import_obsidian5 = require("obsidian");
var OverrideModal = class extends import_obsidian5.Modal {
  constructor(plugin, file, setting) {
    super(plugin.app);
    this.plugin = plugin;
    this.file = file;
    this.setting = setting;
  }
  displaySw(cont) {
    cont.findAll(".setting-item").forEach((el) => {
      var _a;
      if ((_a = el.getAttr("class")) == null ? void 0 : _a.includes("override_root_folder_set")) {
        if (this.setting.saveAttE === "obsFolder") {
          el.hide();
        } else {
          el.show();
        }
      }
    });
  }
  onOpen() {
    const { contentEl } = this;
    contentEl.empty();
    contentEl.createEl("h3", {
      text: t("override.title")
    });
    new import_obsidian5.Setting(contentEl).setName(t("settings.rootPath.name")).setDesc(t("settings.rootPath.desc")).addDropdown(
      (text) => text.addOption(`${SETTINGS_ROOT_OBSFOLDER}`, t("settings.rootPath.options.obsidian")).addOption(`${SETTINGS_ROOT_INFOLDER}`, t("settings.rootPath.options.inFolder")).addOption(`${SETTINGS_ROOT_NEXTTONOTE}`, t("settings.rootPath.options.nextToNote")).setValue(this.setting.saveAttE).onChange(async (value) => {
        this.setting.saveAttE = value;
        this.displaySw(contentEl);
      })
    );
    new import_obsidian5.Setting(contentEl).setName(t("settings.rootFolder.name")).setClass("override_root_folder_set").addText(
      (text) => text.setPlaceholder(DEFAULT_SETTINGS.attachPath.attachmentRoot).setValue(this.setting.attachmentRoot).onChange(async (value) => {
        debugLog("override - attachment root:" + value);
        this.setting.attachmentRoot = value;
      })
    );
    new import_obsidian5.Setting(contentEl).setName(t("settings.attachmentPath.name")).setDesc(t("settings.attachmentPath.desc")).addText(
      (text) => text.setPlaceholder(DEFAULT_SETTINGS.attachPath.attachmentPath).setValue(this.setting.attachmentPath).onChange(async (value) => {
        debugLog("override - attachment path:" + value);
        this.setting.attachmentPath = value;
      })
    );
    new import_obsidian5.Setting(contentEl).setName(t("settings.attachmentFormat.name")).setDesc(t("settings.attachmentFormat.desc")).addText(
      (text) => text.setPlaceholder(DEFAULT_SETTINGS.attachPath.attachFormat).setValue(this.setting.attachFormat).onChange(async (value) => {
        debugLog("override - attachment format:" + value);
        this.setting.attachFormat = value;
      })
    );
    new import_obsidian5.Setting(contentEl).addButton((btn) => {
      btn.setButtonText(t("override.addExtensionOverrides")).onClick(async () => {
        if (this.setting.extensionOverride === void 0) {
          this.setting.extensionOverride = [];
        }
        this.setting.extensionOverride.push({
          extension: "",
          saveAttE: this.setting.saveAttE,
          attachmentRoot: this.setting.attachmentRoot,
          attachmentPath: this.setting.attachmentPath,
          attachFormat: this.setting.attachFormat
        });
        this.onOpen();
      });
    });
    if (this.setting.extensionOverride !== void 0) {
      this.setting.extensionOverride.forEach((ext) => {
        new import_obsidian5.Setting(contentEl).setName(t("override.extension.name")).setDesc(t("override.extension.desc")).setClass("override_extension_set").addText(
          (text) => text.setPlaceholder(t("override.extension.placeholder")).setValue(ext.extension).onChange(async (value) => {
            ext.extension = value;
          })
        ).addButton((btn) => {
          btn.setIcon("trash").onClick(async () => {
            var _a, _b, _c;
            const index = (_b = (_a = this.setting.extensionOverride) == null ? void 0 : _a.indexOf(ext)) != null ? _b : -1;
            (_c = this.setting.extensionOverride) == null ? void 0 : _c.splice(index, 1);
            this.onOpen();
          });
        }).addButton((btn) => {
          btn.setIcon("pencil").onClick(async () => {
            new OverrideExtensionModal(this.plugin, ext, (result) => {
              ext = result;
            }).open();
          });
        });
      });
    }
    new import_obsidian5.Setting(contentEl).addButton((btn) => {
      btn.setButtonText(t("override.buttons.reset")).onClick(async () => {
        this.setting = this.plugin.settings.attachPath;
        delete this.plugin.settings.overridePath[this.file.path];
        await this.plugin.saveSettings();
        await this.plugin.loadSettings();
        new import_obsidian5.Notice(t("override.notifications.reset", { path: this.file.path }));
        this.close();
      });
    }).addButton(
      (btn) => btn.setButtonText(t("override.buttons.submit")).setCta().onClick(async () => {
        if (this.file instanceof import_obsidian5.TFile) {
          this.setting.type = "FILE" /* FILE */;
        } else if (this.file instanceof import_obsidian5.TFolder) {
          this.setting.type = "FOLDER" /* FOLDER */;
        }
        this.plugin.settings.overridePath[this.file.path] = this.setting;
        await this.plugin.saveSettings();
        debugLog("override - overriding settings:", this.file.path, this.setting);
        new import_obsidian5.Notice(t("override.notifications.overridden", { path: this.file.path }));
        this.close();
      })
    );
    this.displaySw(contentEl);
  }
  onClose() {
    const { contentEl } = this;
    contentEl.empty();
  }
};

// src/model/confirm.ts
var import_obsidian10 = require("obsidian");

// src/arrange.ts
var import_obsidian9 = require("obsidian");

// src/lib/path.ts
var path = {
  // Credit: [@creationix/path.js](https://gist.github.com/creationix/7435851)
  join(...partSegments) {
    let parts = [];
    for (let i = 0, l = partSegments.length; i < l; i++) {
      parts = parts.concat(partSegments[i].split("/"));
    }
    const newParts = [];
    for (let i = 0, l = parts.length; i < l; i++) {
      const part = parts[i];
      if (!part || part === ".")
        continue;
      if (part === "..")
        newParts.pop();
      else
        newParts.push(part);
    }
    if (parts[0] === "")
      newParts.unshift("");
    return newParts.join("/");
  },
  // A simple function to get the dirname of a path
  // Trailing slashes are ignored. Leading slash is preserved.
  dirname(filepath) {
    return this.join(filepath, "..");
  },
  // returns the last part of a path, e.g. 'foo.jpg'
  basename(filepath, extension = "") {
    const sp = filepath.split("/");
    const filename = sp[sp.length - 1];
    if (extension !== "") {
      return filename.slice(0, filename.length - extension.length - 1);
    }
    return sp[sp.length - 1];
  },
  // return extension without dot, e.g. 'jpg'
  extname(filepath) {
    const positions = [...filepath.matchAll(new RegExp("\\.", "gi"))].map((a) => a.index);
    const idx = positions[positions.length - 1];
    if (idx === void 0) {
      return "";
    }
    return filepath.slice(idx + 1);
  }
};

// src/override.ts
var import_obsidian6 = require("obsidian");
function getOverrideSetting(settings, file, oldPath = "") {
  if (Object.keys(settings.overridePath).length === 0) {
    return { settingPath: "", setting: settings.attachPath };
  }
  const candidates = {};
  let fileType;
  let filePath;
  fileType = file instanceof import_obsidian6.TFile;
  fileType = !(file instanceof import_obsidian6.TFolder);
  if (oldPath === "") {
    filePath = file.path;
  } else {
    filePath = oldPath;
  }
  for (const overridePath of Object.keys(settings.overridePath)) {
    const overrideSetting = settings.overridePath[overridePath];
    if (fileType) {
      if (overridePath === filePath && overrideSetting.type === "FILE" /* FILE */) {
        return { settingPath: overridePath, setting: overrideSetting };
      } else if (filePath.startsWith(overridePath) && filePath.charAt(overridePath.length) === "/" && overrideSetting.type === "FOLDER" /* FOLDER */) {
        candidates[overridePath] = overrideSetting;
      }
    } else {
      if (overridePath === filePath && overrideSetting.type === "FOLDER" /* FOLDER */) {
        return { settingPath: overridePath, setting: overrideSetting };
      } else if (filePath.startsWith(overridePath) && filePath.charAt(overridePath.length) === "/" && overrideSetting.type === "FOLDER" /* FOLDER */) {
        candidates[overridePath] = overrideSetting;
      }
    }
  }
  if (Object.keys(candidates).length === 0) {
    return { settingPath: "", setting: settings.attachPath };
  }
  const sortedK = Object.keys(candidates).sort(
    (a, b) => a.split("/").length > b.split("/").length ? -1 : a.split("/").length < b.split("/").length ? 1 : 0
  );
  debugLog("getOverrideSetting - sortedK:", sortedK);
  for (const k of sortedK) {
    if (filePath.startsWith(k)) {
      return { settingPath: k, setting: candidates[k] };
    }
  }
  return { settingPath: "", setting: settings.attachPath };
}
function getRenameOverrideSetting(settings, file, oldPath) {
  if (Object.keys(settings.overridePath).length === 0) {
    return { settingPath: "", setting: settings.attachPath };
  }
  const { settingPath: np, setting: ns } = getOverrideSetting(settings, file);
  const { settingPath: op, setting: os } = getOverrideSetting(settings, file, oldPath);
  if (ns.type === "GLOBAL" /* GLOBAL */) {
    return { settingPath: op, setting: os };
  }
  if (os.type === "GLOBAL" /* GLOBAL */) {
    return { settingPath: np, setting: ns };
  }
  if (ns.type === "FILE" /* FILE */ && os.type === "FILE" /* FILE */) {
    debugLog("getRenameOverrideSetting - both file type setting", np, op);
    return { settingPath: "", setting: settings.attachPath };
  }
  if (ns.type === "FILE" /* FILE */ && os.type === "FOLDER" /* FOLDER */) {
    return { settingPath: np, setting: ns };
  } else if (ns.type === "FOLDER" /* FOLDER */ && os.type === "FILE" /* FILE */) {
    return { settingPath: op, setting: os };
  }
  if (ns.type === "FOLDER" /* FOLDER */ && os.type === "FOLDER" /* FOLDER */) {
    const l = np.split("/").length;
    const r = op.split("/").length;
    if (l > r) {
      return { settingPath: np, setting: ns };
    } else if (l < r) {
      return { settingPath: op, setting: os };
    } else if (l === r) {
      return { settingPath: "", setting: os };
    }
  }
  return { settingPath: "", setting: settings.attachPath };
}
function updateOverrideSetting(settings, file, oldPath) {
  const keys = Object.keys(settings.overridePath);
  if (keys.length === 0 || file.path === oldPath) {
    return;
  }
  const { settingPath, setting } = getOverrideSetting(settings, file, oldPath);
  const copySetting = Object.assign({}, setting);
  if (file.path === settingPath) {
    return;
  }
  if (oldPath === settingPath) {
    settings.overridePath[file.path] = copySetting;
    delete settings.overridePath[settingPath];
    return;
  } else {
    const { stripedSrc, stripedDst } = stripPaths(oldPath, file.path);
    if (stripedSrc === settingPath) {
      settings.overridePath[stripedDst] = copySetting;
      delete settings.overridePath[settingPath];
      return;
    }
  }
}
function deleteOverrideSetting(settings, file) {
  const keys = Object.keys(settings.overridePath);
  for (const key of keys) {
    if (file.path === key) {
      delete settings.overridePath[key];
      return true;
    }
  }
  return false;
}

// src/lib/linkDetector.ts
var getAllLinkMatchesInFile = async (mdFile, app2, fileText) => {
  const linkMatches = [];
  if (fileText === void 0) {
    fileText = await app2.vault.read(mdFile);
  }
  const wikiRegex = /\[\[.*?\]\]/g;
  const wikiMatches = fileText.match(wikiRegex);
  if (wikiMatches) {
    const fileRegex = /(?<=\[\[).*?(?=(\]|\|))/;
    for (const wikiMatch of wikiMatches) {
      if (matchIsWikiTransclusion(wikiMatch)) {
        const fileName = getTransclusionFileName(wikiMatch);
        const file = app2.metadataCache.getFirstLinkpathDest(fileName, mdFile.path);
        if (fileName !== "") {
          const linkMatch = {
            type: "wikiTransclusion",
            match: wikiMatch,
            linkText: file ? file.path : fileName,
            sourceFilePath: mdFile.path
          };
          linkMatches.push(linkMatch);
          continue;
        }
      }
      const fileMatch = wikiMatch.match(fileRegex);
      if (fileMatch) {
        if (fileMatch[0].startsWith("http"))
          continue;
        const file = app2.metadataCache.getFirstLinkpathDest(fileMatch[0], mdFile.path);
        const linkMatch = {
          type: "wiki",
          match: wikiMatch,
          linkText: file ? file.path : fileMatch[0],
          sourceFilePath: mdFile.path
        };
        linkMatches.push(linkMatch);
      }
    }
  }
  const markdownRegex = /\[(^$|.*?)\]\((.*?)\)/g;
  const markdownMatches = fileText.match(markdownRegex);
  if (markdownMatches) {
    const fileRegex = /(?<=\().*(?=\))/;
    for (const markdownMatch of markdownMatches) {
      if (matchIsMdTransclusion(markdownMatch)) {
        const fileName = getTransclusionFileName(markdownMatch);
        const file = app2.metadataCache.getFirstLinkpathDest(fileName, mdFile.path);
        if (fileName !== "") {
          const linkMatch = {
            type: "mdTransclusion",
            match: markdownMatch,
            linkText: file ? file.path : fileName,
            sourceFilePath: mdFile.path
          };
          linkMatches.push(linkMatch);
          continue;
        }
      }
      const fileMatch = markdownMatch.match(fileRegex);
      if (fileMatch) {
        if (fileMatch[0].startsWith("http"))
          continue;
        const file = app2.metadataCache.getFirstLinkpathDest(fileMatch[0], mdFile.path);
        const linkMatch = {
          type: "markdown",
          match: markdownMatch,
          linkText: file ? file.path : fileMatch[0],
          sourceFilePath: mdFile.path
        };
        linkMatches.push(linkMatch);
      }
    }
  }
  return linkMatches;
};
var wikiTransclusionRegex = /\[\[(.*?)#.*?\]\]/;
var wikiTransclusionFileNameRegex = /(?<=\[\[)(.*)(?=#)/;
var mdTransclusionRegex = /\[.*?]\((.*?)#.*?\)/;
var mdTransclusionFileNameRegex = /(?<=\]\()(.*)(?=#)/;
var matchIsWikiTransclusion = (match) => {
  return wikiTransclusionRegex.test(match);
};
var matchIsMdTransclusion = (match) => {
  return mdTransclusionRegex.test(match);
};
var getTransclusionFileName = (match) => {
  const isWiki = wikiTransclusionRegex.test(match);
  const isMd = mdTransclusionRegex.test(match);
  if (isWiki || isMd) {
    const fileNameMatch = match.match(isWiki ? wikiTransclusionFileNameRegex : mdTransclusionFileNameRegex);
    if (fileNameMatch)
      return fileNameMatch[0];
  }
  return "";
};

// src/lib/deduplicate.ts
function escapeRegExp(s) {
  return s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
async function deduplicateNewName(newName, file) {
  const dir = file.path;
  const listed = await this.app.vault.adapter.list(dir);
  debugLog("deduplicateNewName - sibling files", listed);
  const newNameExt = path.extname(newName), newNameStem = newName.slice(0, newName.length - newNameExt.length - 1), newNameStemEscaped = escapeRegExp(newNameStem), delimiter = "-", delimiterEscaped = escapeRegExp(delimiter);
  const dupNameRegex = new RegExp(
    `^(?<name>${newNameStemEscaped})${delimiterEscaped}(?<number>\\d{1,3})\\.${newNameExt}$`
  );
  debugLog("dupNameRegex", dupNameRegex);
  const dupNameNumbers = [];
  let isNewNameExist = false;
  for (let sibling of listed.files) {
    sibling = path.basename(sibling);
    if (sibling == newName) {
      isNewNameExist = true;
      continue;
    }
    const m = dupNameRegex.exec(sibling);
    if (!m || m.groups === void 0)
      continue;
    dupNameNumbers.push(parseInt(m.groups.number));
  }
  if (isNewNameExist) {
    const newNumber = dupNameNumbers.length > 0 ? Math.max(...dupNameNumbers) + 1 : 1;
    newName = `${newNameStem}${delimiter}${newNumber}.${newNameExt}`;
  }
  return {
    name: newName,
    basename: newName.slice(0, newName.length - newNameExt.length - 1),
    extension: newNameExt
  };
}

// src/settings/metadata.ts
var import_obsidian8 = require("obsidian");

// src/commons.ts
var import_obsidian7 = require("obsidian");
function getActiveFile(app2) {
  const view = getActiveView(app2);
  if (view == null) {
    return void 0;
  } else if (view.file == null) {
    return void 0;
  } else {
    return view.file;
  }
}
function getActiveView(app2) {
  return app2.workspace.getActiveViewOfType(import_obsidian7.TextFileView);
}
function getRootPath(notePath, setting) {
  let root;
  const obsmediadir = app.vault.getConfig("attachmentFolderPath");
  switch (setting.saveAttE) {
    case `${SETTINGS_ROOT_INFOLDER}`:
      root = path.join(setting.attachmentRoot);
      break;
    case `${SETTINGS_ROOT_NEXTTONOTE}`:
      root = path.join(notePath, setting.attachmentRoot.replace("./", ""));
      break;
    default:
      if (obsmediadir === "/") {
        root = obsmediadir;
      } else if (obsmediadir === "./") {
        root = path.join(notePath);
      } else if (obsmediadir.match(/\.\/.+/g) !== null) {
        root = path.join(notePath, obsmediadir.replace("./", ""));
      } else {
        root = obsmediadir;
      }
  }
  return root === "/" ? root : (0, import_obsidian7.normalizePath)(root);
}
async function checkEmptyFolder(adapter, path2) {
  const exist = await adapter.exists(path2, true);
  if (!exist) {
    return true;
  }
  const data = await adapter.list(path2);
  if (data.files.length > 0) {
    return false;
  }
  if (data.folders.length > 0) {
    for (let i = 0; i < data.folders.length; i++) {
      return checkEmptyFolder(adapter, data.folders[i]);
    }
  }
  return true;
}

// src/settings/metadata.ts
var Metadata = class {
  constructor(path2, name, basename, extension, parentPath, parentName, attachmentFile) {
    /** parent path of file */
    this.parentPath = "";
    /** parent path basename of file */
    this.parentName = "";
    this.path = path2;
    this.name = name;
    this.basename = basename;
    this.extension = extension;
    this.parentPath = parentPath;
    this.parentName = parentName;
    this.attachmentFile = attachmentFile;
  }
  /**
   * Returns a formatted attachment file name according to the provided settings.
   *
   * @param {AttachmentPathSettings} setting - attachment path settings object
   * @param {string} dateFormat - format string for date and time
   * @param {string} originalName - name of the original attachment
   * @param {string} [linkName] - optional name for the attachment link
   * @return {string} the formatted attachment file name
   */
  async getAttachFileName(setting, dateFormat, originalName, adapter, linkName) {
    const dateTime = window.moment().format(dateFormat);
    let md5 = "";
    let attachFormat = "";
    if (this.attachmentFile !== void 0) {
      md5 = await md5sum(adapter, this.attachmentFile);
      const { extSetting } = getExtensionOverrideSetting(this.attachmentFile.extension, setting);
      if (extSetting !== void 0) {
        attachFormat = extSetting.attachFormat;
      } else {
        attachFormat = setting.attachFormat;
      }
    }
    if (attachFormat.includes(SETTINGS_VARIABLES_ORIGINALNAME)) {
      if (originalName === "" && linkName != void 0) {
        return linkName;
      } else {
        return attachFormat.replace(`${SETTINGS_VARIABLES_DATES}`, dateTime).replace(`${SETTINGS_VARIABLES_NOTENAME}`, this.basename).replace(`${SETTINGS_VARIABLES_ORIGINALNAME}`, originalName).replace(`${SETTINGS_VARIABLES_MD5}`, md5);
      }
    }
    return attachFormat.replace(`${SETTINGS_VARIABLES_DATES}`, dateTime).replace(`${SETTINGS_VARIABLES_NOTENAME}`, this.basename).replace(`${SETTINGS_VARIABLES_MD5}`, md5);
  }
  /**
   * Returns the attachment path based on the given AttachmentPathSettings object.
   *
   * @param {AttachmentPathSettings} setting - An object containing the attachment path settings.
   * @return {string} The normalized attachment path.
   */
  getAttachmentPath(setting, dateFormat) {
    const dateTime = window.moment().format(dateFormat);
    let root = "";
    let attachPath = "";
    if (this.attachmentFile !== void 0) {
      const { extSetting } = getExtensionOverrideSetting(this.attachmentFile.extension, setting);
      if (extSetting !== void 0) {
        root = getRootPath(this.parentPath, extSetting);
        attachPath = path.join(
          root,
          extSetting.attachmentPath.replace(`${SETTINGS_VARIABLES_NOTEPATH}`, this.parentPath).replace(`${SETTINGS_VARIABLES_NOTENAME}`, this.basename).replace(`${SETTINGS_VARIABLES_NOTEPARENT}`, this.parentName).replace(`${SETTINGS_VARIABLES_DATES}`, dateTime)
        );
        return (0, import_obsidian8.normalizePath)(attachPath);
      }
    }
    root = getRootPath(this.parentPath, setting);
    debugLog("getAttachmentPath - root", root);
    attachPath = path.join(
      root,
      setting.attachmentPath.replace(`${SETTINGS_VARIABLES_NOTEPATH}`, this.parentPath).replace(`${SETTINGS_VARIABLES_NOTENAME}`, this.basename).replace(`${SETTINGS_VARIABLES_NOTEPARENT}`, this.parentName).replace(`${SETTINGS_VARIABLES_DATES}`, dateTime)
    );
    return (0, import_obsidian8.normalizePath)(attachPath);
  }
};
function getMetadata(file, attach) {
  const parentPath = path.dirname(file);
  const parentName = path.basename(parentPath);
  const name = path.basename(file);
  const extension = path.extname(file);
  const basename = path.basename(file, extension);
  return new Metadata(file, name, basename, extension, parentPath, parentName, attach);
}

// src/exclude.ts
function isExcluded(path2, settings) {
  debugLog("excludePathsArray: ", settings.excludePathsArray);
  for (const excludedPath of settings.excludePathsArray) {
    if (excludedPath.length === 0) {
      continue;
    }
    if (settings.excludeSubpaths && path2.startsWith(excludedPath)) {
      debugLog("isExcluded: ", path2);
      return true;
    } else {
      if (path2 === excludedPath) {
        return true;
      }
    }
  }
  return false;
}

// src/lib/originalStorage.ts
function containOriginalNameVariable(setting, ext) {
  const { extSetting } = getExtensionOverrideSetting(ext, setting);
  if (extSetting !== void 0 && extSetting.attachFormat.contains(SETTINGS_VARIABLES_ORIGINALNAME) || setting.attachFormat.contains(SETTINGS_VARIABLES_ORIGINALNAME)) {
    return true;
  }
  return false;
}
function saveOriginalName(settings, setting, ext, data) {
  if (settings.originalNameStorage === void 0) {
    settings.originalNameStorage = [];
  }
  if (containOriginalNameVariable(setting, ext)) {
    settings.originalNameStorage.filter((n) => n.md5 == data.md5).forEach((n) => settings.originalNameStorage.remove(n));
    settings.originalNameStorage.push(data);
  }
}
function loadOriginalName(settings, setting, ext, md5) {
  if (containOriginalNameVariable(setting, ext)) {
    const first = settings.originalNameStorage.find((data) => data.md5 === md5);
    const last = settings.originalNameStorage.reverse().find((data) => data.md5 === md5);
    if (first === void 0 || last == void 0) {
      return void 0;
    }
    if (first.md5 === last.md5 && first.n === last.n) {
      return last;
    } else if (first.md5 === last.md5 && first.n !== last.n) {
      settings.originalNameStorage.remove(first);
      return last;
    }
  }
  return void 0;
}

// src/arrange.ts
var bannerRegex = /!\[\[(.*?)\]\]/i;
var ArrangeHandler = class {
  constructor(settings, app2, plugin) {
    this.settings = settings;
    this.app = app2;
    this.plugin = plugin;
  }
  /**
   * Rearranges attachments that are linked by markdown or canvas.
   * Only rearranges attachments if autoRenameAttachment is enabled in settings.
   *
   * @param {RearrangeType} type - The type of attachments to rearrange.
   * @param {TFile} file - The file to which the attachments are linked (optional), if the type was "file", thi should be provided.
   * @param {string} oldPath - The old path of the file (optional), used for rename event.
   */
  async rearrangeAttachment(type, file, oldPath) {
    var _a;
    if (!this.settings.autoRenameAttachment) {
      debugLog("rearrangeAttachment - autoRenameAttachment not enable");
      return;
    }
    const attachments = await this.getAttachmentsInVault(this.settings, type, file, oldPath);
    debugLog("rearrangeAttachment - attachments:", Object.keys(attachments).length, Object.entries(attachments));
    for (const obNote of Object.keys(attachments)) {
      const innerFile = this.app.vault.getAbstractFileByPath(obNote);
      if (!(innerFile instanceof import_obsidian9.TFile) || isAttachment(this.settings, innerFile)) {
        debugLog(`rearrangeAttachment - ${obNote} not exists or is attachment, skipped`);
        continue;
      }
      const { setting } = getOverrideSetting(this.settings, innerFile);
      if (attachments[obNote].size == 0) {
        continue;
      }
      const md = getMetadata(obNote);
      const attachPath = md.getAttachmentPath(setting, this.settings.dateFormat);
      if (!await this.app.vault.adapter.exists(attachPath, true)) {
        if (oldPath != void 0 && await this.app.vault.adapter.exists(attachPath, false)) {
          const mdOld = getMetadata(oldPath);
          const attachPathOld = mdOld.getAttachmentPath(setting, this.settings.dateFormat);
          this.app.vault.adapter.rename(attachPathOld, attachPath);
        } else {
          await this.app.vault.adapter.mkdir(attachPath);
        }
      }
      for (let link of attachments[obNote]) {
        try {
          link = decodeURI(link);
        } catch (err) {
          console.log(`Invalid link: ${link}, err: ${err}`);
          continue;
        }
        debugLog(`rearrangeAttachment - article: ${obNote} links: ${link}`);
        const linkFile = this.app.vault.getAbstractFileByPath(link);
        if (linkFile === null || !(linkFile instanceof import_obsidian9.TFile)) {
          debugLog(`${link} not exists, skipped`);
          continue;
        }
        const metadata = getMetadata(obNote, linkFile);
        const md5 = await md5sum(this.app.vault.adapter, linkFile);
        const originalName = loadOriginalName(this.settings, setting, linkFile.extension, md5);
        debugLog("rearrangeAttachment - original name:", originalName);
        let attachName = "";
        if (containOriginalNameVariable(setting, linkFile.extension)) {
          attachName = await metadata.getAttachFileName(
            setting,
            this.settings.dateFormat,
            (_a = originalName == null ? void 0 : originalName.n) != null ? _a : "",
            this.app.vault.adapter,
            path.basename(link, path.extname(link))
          );
        } else {
          attachName = await metadata.getAttachFileName(
            setting,
            this.settings.dateFormat,
            path.basename(link, path.extname(link)),
            this.app.vault.adapter
          );
        }
        if (attachPath == path.dirname(link) && attachName === path.basename(link, path.extname(link))) {
          continue;
        }
        const attachPathFile = this.app.vault.getAbstractFileByPath(attachPath);
        if (attachPathFile === null || !(attachPathFile instanceof import_obsidian9.TFolder)) {
          debugLog(`${attachPath} not exists, skipped`);
          continue;
        }
        const { name } = await deduplicateNewName(attachName + "." + path.extname(link), attachPathFile);
        debugLog("rearrangeAttachment - deduplicated name:", name);
        await this.app.fileManager.renameFile(linkFile, path.join(attachPath, name));
      }
    }
  }
  /**
   * Retrieves the attachments in the vault based on the specified settings and type.
   * If a file is provided, only attachments related to that file will be returned.
   *
   * @param {AttachmentManagementPluginSettings} settings - The settings for the attachment management plugin.
   * @param {RearrangeType} type - The type of attachments to retrieve.
   * @param {TFile} [file] - The file to filter attachments by. Optional.
   * @return {Promise<Record<string, Set<string>>>} - A promise that resolves to a record of attachments, where each key is a file name and each value is a set of associated attachment names.
   */
  async getAttachmentsInVault(settings, type, file, oldPath) {
    let attachmentsRecord = {};
    attachmentsRecord = await this.getAttachmentsInVaultByLinks(settings, type, file, oldPath);
    return attachmentsRecord;
  }
  /**
   * Modified from https://github.com/ozntel/oz-clear-unused-images-obsidian/blob/master/src/util.ts#LL48C21-L48C21
   * Retrieves a record of attachments in the vault based on the given settings and type.
   *
   * @param {AttachmentManagementPluginSettings} settings - The settings for the attachment management plugin.
   * @param {RearrangeType} type - The type of attachments to retrieve.
   * @param {TFile} file - The file to retrieve attachments for (optional).
   * @return {Promise<Record<string, Set<string>>>} - A promise that resolves to a record of attachments.
   */
  async getAttachmentsInVaultByLinks(settings, type, file, oldPath) {
    const attachmentsRecord = {};
    let resolvedLinks = {};
    let allFiles = [];
    if (type == 1 /* LINKS */) {
      resolvedLinks = this.app.metadataCache.resolvedLinks;
      allFiles = this.app.vault.getFiles();
    } else if (type == 0 /* ACTIVE */) {
      const file2 = getActiveFile(this.app);
      if (file2) {
        if (file2.parent && isExcluded(file2.parent.path, this.settings) || isAttachment(this.settings, file2)) {
          allFiles = [];
          new import_obsidian9.Notice(`${file2.path} was excluded, skipped`);
        } else {
          debugLog("getAttachmentsInVaultByLinks - active:", file2.path);
          allFiles = [file2];
          if (this.app.metadataCache.resolvedLinks[file2.path]) {
            resolvedLinks[file2.path] = this.app.metadataCache.resolvedLinks[file2.path];
          }
          debugLog("getAttachmentsInVaultByLinks - resolvedLinks:", resolvedLinks);
        }
      }
    } else if (type == 2 /* FILE */ && file != void 0) {
      if (file.parent && isExcluded(file.parent.path, this.settings) || isAttachment(this.settings, file)) {
        allFiles = [];
        new import_obsidian9.Notice(`${file.path} was excluded, skipped`);
      } else {
        debugLog("getAttachmentsInVaultByLinks - file:", file.path);
        allFiles = [file];
        const rlinks = this.app.metadataCache.resolvedLinks[file.path];
        if (rlinks) {
          debugLog("getAttachmentsInVaultByLinks - rlinks:", rlinks);
          resolvedLinks[file.path] = rlinks;
        } else if (oldPath) {
          debugLog("getAttachmentsInVaultByLinks - oldPath:", oldPath);
          resolvedLinks[file.path] = this.app.metadataCache.resolvedLinks[oldPath];
        }
        debugLog("getAttachmentsInVaultByLinks - resolvedLinks:", resolvedLinks);
      }
    }
    debugLog("getAttachmentsInVaultByLinks - allFiles:", allFiles.length, allFiles);
    if (resolvedLinks) {
      for (const [mdFile, links] of Object.entries(resolvedLinks)) {
        const attachmentsSet = /* @__PURE__ */ new Set();
        if (links) {
          for (const [filePath] of Object.entries(links)) {
            if (isAttachment(settings, filePath)) {
              this.addToSet(attachmentsSet, filePath);
            }
          }
          this.addToRecord(attachmentsRecord, mdFile, attachmentsSet);
        }
      }
    }
    for (let i = 0; i < allFiles.length; i++) {
      const obsFile = allFiles[i];
      const attachmentsSet = /* @__PURE__ */ new Set();
      if (obsFile.parent && isExcluded(obsFile.parent.path, this.settings)) {
        continue;
      }
      if (isMarkdownFile(obsFile.extension)) {
        const fileCache = this.app.metadataCache.getFileCache(obsFile);
        if (fileCache === null) {
          continue;
        }
        if (fileCache.frontmatter) {
          const frontmatter = fileCache.frontmatter;
          for (const k of Object.keys(frontmatter)) {
            if (typeof frontmatter[k] === "string") {
              const formatMatch = frontmatter[k].match(bannerRegex);
              if (formatMatch && formatMatch[1]) {
                const fileName = formatMatch[1];
                const file2 = this.app.metadataCache.getFirstLinkpathDest(fileName, obsFile.path);
                if (file2 && isAttachment(settings, file2.path)) {
                  this.addToSet(attachmentsSet, file2.path);
                }
              }
            }
          }
        }
        const linkMatches = await getAllLinkMatchesInFile(obsFile, app);
        for (const linkMatch of linkMatches) {
          if (isAttachment(settings, linkMatch.linkText)) {
            this.addToSet(attachmentsSet, linkMatch.linkText);
          }
        }
      } else if (isCanvasFile(obsFile.extension)) {
        const fileRead = await this.app.vault.cachedRead(obsFile);
        if (!fileRead || fileRead.length === 0) {
          continue;
        }
        let canvasData;
        try {
          canvasData = JSON.parse(fileRead);
        } catch (e) {
          debugLog("getAttachmentsInVaultByLinks - parse canvas data error", e);
          continue;
        }
        if (canvasData.nodes && canvasData.nodes.length > 0) {
          for (const node of canvasData.nodes) {
            if (node.type === "file") {
              if (isAttachment(settings, node.file)) {
                this.addToSet(attachmentsSet, node.file);
              }
            } else if (node.type == "text") {
              const linkMatches = await getAllLinkMatchesInFile(obsFile, app, node.text);
              for (const linkMatch of linkMatches) {
                if (isAttachment(settings, linkMatch.linkText)) {
                  this.addToSet(attachmentsSet, linkMatch.linkText);
                }
              }
            }
          }
        }
      }
      this.addToRecord(attachmentsRecord, obsFile.path, attachmentsSet);
    }
    return attachmentsRecord;
  }
  addToRecord(record, key, value) {
    if (record[key] === void 0) {
      record[key] = value;
      return;
    }
    const valueSet = record[key];
    for (const val of value) {
      this.addToSet(valueSet, val);
    }
    record[key] = valueSet;
  }
  addToSet(setObj, value) {
    if (!setObj.has(value)) {
      setObj.add(value);
    }
  }
  needToRename(settings, attachPath, attachName, noteName, link) {
    const linkPath = path.dirname(link);
    const linkName = path.basename(link, path.extname(link));
    if (linkName.length !== attachName.length) {
      return true;
    }
    if (attachPath !== linkPath) {
      return true;
    } else {
      if (settings.attachFormat.includes(SETTINGS_VARIABLES_NOTENAME) && !linkName.includes(noteName)) {
        return true;
      }
      const noNoteNameAttachFormat = settings.attachFormat.split(SETTINGS_VARIABLES_NOTENAME);
      if (settings.attachFormat.includes(SETTINGS_VARIABLES_DATES)) {
        for (const formatPart in noNoteNameAttachFormat) {
          const splited = formatPart.split(SETTINGS_VARIABLES_DATES);
          for (const part in splited) {
            if (!linkName.includes(part)) {
              return true;
            }
          }
        }
      }
    }
    return false;
  }
};

// src/model/confirm.ts
var ConfirmModal = class extends import_obsidian10.Modal {
  constructor(plugin) {
    super(plugin.app);
    this.plugin = plugin;
  }
  onOpen() {
    const { contentEl } = this;
    contentEl.empty();
    contentEl.createEl("h3", {
      text: t("confirm.title")
    });
    contentEl.createSpan("", (el) => {
      el.innerText = t("confirm.message");
    });
    new import_obsidian10.Setting(contentEl).addButton((btn) => {
      btn.setButtonText(t("common.cancel")).setCta().onClick(() => {
        this.close();
      });
    }).addButton(
      (btn) => btn.setButtonText(t("confirm.continue")).onClick(async () => {
        new ArrangeHandler(this.plugin.settings, this.plugin.app, this.plugin).rearrangeAttachment(1 /* LINKS */).finally(() => {
          new import_obsidian10.Notice(t("notifications.arrangeCompleted"));
          this.close();
        });
      })
    );
  }
  onClose() {
    const { contentEl } = this;
    contentEl.empty();
  }
};

// src/create.ts
var import_obsidian11 = require("obsidian");
var CreateHandler = class {
  constructor(plugin, settings) {
    this.plugin = plugin;
    this.app = this.plugin.app;
    this.settings = settings;
  }
  /**
   * Post-processing of created attachment file (for paste and drop event).
   * @param attach - the attachment file to process
   * @param source - the notes file that linked to attach
   * @returns - none
   */
  processAttach(attach, source) {
    if (source.parent && isExcluded(source.parent.path, this.settings)) {
      debugLog("processAttach - not a file or exclude path:", source.path);
      new import_obsidian11.Notice(`${source.path} was excluded, skipped`);
      return;
    }
    const { setting } = getOverrideSetting(this.settings, source);
    const { extSetting } = getExtensionOverrideSetting(attach.extension, setting);
    debugLog("processAttach - file.extension:", attach.extension);
    if (extSetting === void 0 && !isImage(attach.extension) && !isPastedImage(attach)) {
      debugLog("renameFiles - no handle extension:", attach.extension);
      return;
    }
    const metadata = getMetadata(source.path, attach);
    debugLog("processAttach - metadata:", metadata);
    const attachPath = metadata.getAttachmentPath(setting, this.settings.dateFormat);
    metadata.getAttachFileName(setting, this.settings.dateFormat, attach.basename, this.app.vault.adapter).then((attachName) => {
      attachName = attachName + "." + attach.extension;
      this.app.vault.adapter.exists(attachPath, true).then(async (exists) => {
        if (!exists) {
          await this.app.vault.adapter.mkdir(attachPath);
          debugLog("processAttach - create path:", attachPath);
        }
      }).finally(() => {
        const attachPathFolder = this.app.vault.getAbstractFileByPath(attachPath);
        deduplicateNewName(attachName, attachPathFolder).then(({ name }) => {
          debugLog("processAttach - new path of file:", path.join(attachPath, name));
          this.renameCreateFile(attach, attachPath, name, source);
        });
      });
    });
  }
  /**
   * Rename the file specified by `@param file`, and update the link of the file if specified updateLink
   * @param attach - file to rename
   * @param attachPath - where to the renamed file will be move to
   * @param attachName - name of the renamed file
   * @param source - associated active file
   * @returns - none
   */
  renameCreateFile(attach, attachPath, attachName, source) {
    const dst = (0, import_obsidian11.normalizePath)(path.join(attachPath, attachName));
    debugLog("renameFile - ", attach.path, " to ", dst);
    const original = attach.basename;
    const name = attach.name;
    this.app.fileManager.renameFile(attach, dst).then(() => {
      new import_obsidian11.Notice(`Renamed ${name} to ${attachName}.`);
    }).finally(() => {
      const { setting } = getOverrideSetting(this.settings, source);
      md5sum(this.app.vault.adapter, attach).then((md5) => {
        saveOriginalName(this.settings, setting, attach.extension, {
          n: original,
          md5
        });
        this.plugin.saveData(this.settings);
      });
    });
  }
};

// src/main.ts
var AttachmentManagementPlugin = class extends import_obsidian12.Plugin {
  constructor() {
    super(...arguments);
    this.createdQueue = [];
  }
  async onload() {
    await this.loadSettings();
    loadAllTranslations();
    const savedLanguage = this.settings.language || detectLanguage();
    setLanguage(savedLanguage);
    initI18n();
    console.log(`Plugin loading: ${this.manifest.name} v.${this.manifest.version}`);
    this.app.workspace.onLayoutReady(() => {
      this.initCommands();
      this.registerEvent(
        this.app.workspace.on("file-menu", async (menu, file) => {
          if (file.parent && isExcluded(file.parent.path, this.settings) || isAttachment(this.settings, file)) {
            return;
          }
          menu.addItem((item) => {
            item.setTitle(t("override.menuTitle")).setIcon("image-plus").onClick(async () => {
              const { setting } = getOverrideSetting(this.settings, file);
              const fileSetting = Object.assign({}, setting);
              this.overrideConfiguration(file, fileSetting);
            });
          });
        })
      );
      this.registerEvent(
        this.app.vault.on("create", async (file) => {
          debugLog("on create event - file:", file.path);
          if (!(file instanceof import_obsidian12.TFile)) {
            return;
          }
          const curentTime = new Date().getTime();
          const timeGapMs = curentTime - file.stat.mtime;
          const timeGapCs = curentTime - file.stat.ctime;
          if (timeGapMs > 1e3 || timeGapCs > 1e3 || isMarkdownFile(file.extension) || isCanvasFile(file.extension)) {
            return;
          }
          if (matchExtension(file.extension, this.settings.excludeExtensionPattern)) {
            debugLog("create - excluded file by extension", file);
            return;
          }
          this.createdQueue.push(file);
        })
      );
      this.registerEvent(
        this.app.vault.on("modify", (file) => {
          debugLog("on modify event - create queue:", this.createdQueue);
          if (this.createdQueue.length < 1 || !(file instanceof import_obsidian12.TFile)) {
            return;
          }
          debugLog("on modify event - file:", file.path);
          this.app.vault.adapter.process(file.path, (pdata) => {
            const f = this.createdQueue.first();
            if (f != void 0) {
              this.app.vault.adapter.exists(f.path, true).then((exist) => {
                if (exist) {
                  const processor = new CreateHandler(this, this.settings);
                  const link = this.app.fileManager.generateMarkdownLink(f, file.path);
                  if (file.extension == "md" && pdata.indexOf(link) != -1 || file.extension == "canvas" && pdata.indexOf(f.path) != -1) {
                    this.createdQueue.remove(f);
                    processor.processAttach(f, file);
                  }
                } else {
                  debugLog("on modify event - file does not exist:", f.path);
                  this.createdQueue.remove(f);
                }
              });
            }
            return pdata;
          });
        })
      );
      this.registerEvent(
        // when trigger a rename event on folder, for each file/folder in this renamed folder (include itself) will trigger this event
        this.app.vault.on("rename", async (file, oldPath) => {
          debugLog("on rename event - new path and old path:", file.path, oldPath);
          const { setting } = getRenameOverrideSetting(this.settings, file, oldPath);
          debugLog("rename - using settings:", setting);
          if (setting.type === "FOLDER" /* FOLDER */ || setting.type === "FILE" /* FILE */) {
            updateOverrideSetting(this.settings, file, oldPath);
            this.saveSettings();
          }
          debugLog("rename - updated settings:", setting);
          if (!this.settings.autoRenameAttachment) {
            debugLog("rename - auto rename not enabled:", this.settings.autoRenameAttachment);
            return;
          }
          if (file instanceof import_obsidian12.TFile) {
            if (file.parent && isExcluded(file.parent.path, this.settings)) {
              debugLog("rename - exclude path:", file.parent.path);
              new import_obsidian12.Notice(t("notifications.fileExcluded", { path: file.path }));
              return;
            }
            if (isAttachment(this.settings, file)) {
              debugLog("rename - not processing rename on attachment:", file.path);
              return;
            }
            await new ArrangeHandler(this.settings, this.app, this).rearrangeAttachment(
              2 /* FILE */,
              file,
              oldPath
            );
            const oldMetadata = getMetadata(oldPath);
            const oldAttachPath = oldMetadata.getAttachmentPath(setting, this.settings.dateFormat);
            this.app.vault.adapter.exists(oldAttachPath, true).then((exists) => {
              if (exists) {
                checkEmptyFolder(this.app.vault.adapter, oldAttachPath).then((empty) => {
                  if (empty) {
                    this.app.vault.adapter.rmdir(oldAttachPath, true);
                  }
                });
              }
            });
          } else if (file instanceof import_obsidian12.TFolder) {
            return;
          }
        })
      );
      this.registerEvent(
        this.app.vault.on("delete", async (file) => {
          debugLog("on delete event - file path:", file.path);
          if (file.parent && isExcluded(file.parent.path, this.settings) || isAttachment(this.settings, file)) {
            debugLog("delete - exclude path or the file is an attachment:", file.path);
            return;
          }
          if (deleteOverrideSetting(this.settings, file)) {
            await this.saveSettings();
            new import_obsidian12.Notice("Removed override setting of " + file.path);
          }
          if (file instanceof import_obsidian12.TFile) {
            const oldMetadata = getMetadata(file.path);
            const { setting } = getOverrideSetting(this.settings, file);
            const oldAttachPath = oldMetadata.getAttachmentPath(setting, this.settings.dateFormat);
            this.app.vault.adapter.exists(oldAttachPath, true).then((exists) => {
              if (exists) {
                checkEmptyFolder(this.app.vault.adapter, oldAttachPath).then((empty) => {
                  if (empty) {
                    this.app.vault.adapter.rmdir(oldAttachPath, true);
                  }
                });
              }
            });
          }
        })
      );
      this.addSettingTab(new AttachmentManagementSettingTab(this.app, this));
    });
  }
  async overrideConfiguration(file, setting) {
    new OverrideModal(this, file, setting).open();
  }
  /**
   * Initializes and registers the plugin's commands
   * This method is responsible for setting up the user interface by adding various commands.
   * These commands include settings overrides, resetting settings, clearing unused original name storage,
   * and rearranging attachments.
   *
   * Note: The actual implementation of each command is not included in this method and needs to be
   * defined separately asynchronously.
   *
   * Warning: Make sure you have checked for errors while implementing the functionality of each command.
   */
  initCommands() {
    this.addCommand({
      id: "attachment-management-rearrange-all-links",
      name: t("commands.rearrangeAllLinks"),
      callback: async () => {
        new ConfirmModal(this).open();
      }
    });
    this.addCommand({
      id: "attachment-management-rearrange-active-links",
      name: t("commands.rearrangeActiveLinks"),
      callback: async () => {
        new ArrangeHandler(this.settings, this.app, this).rearrangeAttachment(0 /* ACTIVE */).finally(() => {
          new import_obsidian12.Notice(t("notifications.arrangeCompleted"));
        });
      }
    });
    this.addCommand({
      id: "attachment-management-override-setting",
      name: "Overriding setting",
      checkCallback: (checking) => {
        const file = getActiveFile(this.app);
        if (file) {
          if (isAttachment(this.settings, file)) {
            return true;
          }
          if (!checking) {
            if (file.parent && isExcluded(file.parent.path, this.settings)) {
              new import_obsidian12.Notice(`${file.path} was excluded`);
              return true;
            }
            const { setting } = getOverrideSetting(this.settings, file);
            const fileSetting = Object.assign({}, setting);
            this.overrideConfiguration(file, fileSetting);
          }
          return true;
        }
        return false;
      }
    });
    this.addCommand({
      id: "attachment-management-reset-override-setting",
      name: t("commands.resetOverrideSetting"),
      checkCallback: (checking) => {
        const file = getActiveFile(this.app);
        if (file) {
          if (isAttachment(this.settings, file)) {
            return true;
          }
          if (!checking) {
            if (file.parent && isExcluded(file.parent.path, this.settings)) {
              new import_obsidian12.Notice(`${file.path} was excluded`);
              return true;
            }
            delete this.settings.overridePath[file.path];
            this.saveSettings().finally(() => {
              new import_obsidian12.Notice(t("notifications.resetAttachmentSetting", { path: file.path }));
            });
          }
          return true;
        }
        return false;
      }
    });
    this.addCommand({
      id: "attachment-management-clear-unused-originalname-storage",
      name: t("commands.clearUnusedStorage"),
      callback: async () => {
        const attachments = await new ArrangeHandler(this.settings, this.app, this).getAttachmentsInVault(
          this.settings,
          1 /* LINKS */
        );
        const storages = [];
        for (const attachs of Object.values(attachments)) {
          for (const attach of attachs) {
            const link = decodeURI(attach);
            const linkFile = this.app.vault.getAbstractFileByPath(link);
            if (linkFile !== null && linkFile instanceof import_obsidian12.TFile) {
              md5sum(this.app.vault.adapter, linkFile).then((md5) => {
                const ret = this.settings.originalNameStorage.find((data) => data.md5 === md5);
                if (ret) {
                  storages.filter((n) => n.md5 == md5).forEach((n) => storages.remove(n));
                  storages.push(ret);
                }
              });
            }
          }
        }
        debugLog("clearUnusedOriginalNameStorage - storage:", storages);
        this.settings.originalNameStorage = storages;
        this.saveSettings();
      }
    });
  }
  async loadSettings() {
    this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
  }
  async saveSettings() {
    await this.saveData(this.settings);
  }
  async onunload() {
    console.log("unloading attachment management.");
    this.createdQueue = [];
  }
};

/* nosourcemap */