var __defProp = Object.defineProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: !0 });
class DCFFileSizeValidator {
  static {
    __name(this, "DCFFileSizeValidator");
  }
  fileInputElement = null;
  errorElement = null;
  errorElementClassList = [];
  formattedSizeOutputs = [];
  sizeLimit = -1;
  validationFailedClass = null;
  constructor(fileInput, options) {
    if ("errorElementClassList" in options && Array.isArray(options.errorElementClassList) && (this.errorElementClassList = options.errorElementClassList), this.fileInputElement = fileInput, this.fileInputElement.getAttribute("id") === null)
      throw new Error("File Input element is missing ID");
    this.formattedSizeOutputs = document.querySelectorAll(`.dcf-file-size-validator-size[data-input="${this.fileInputElement.getAttribute("id")}"]`), this.sizeLimit = this.#parseSize(this.fileInputElement.dataset.maxSize), this.validationFailedClass = this.fileInputElement.dataset.validationFailedClass, this.formattedSizeOutputs.forEach((singleOutputElement) => {
      singleOutputElement.innerHTML = this.#formatSize(this.sizeLimit);
    }), this.errorElement = document.createElement("div"), this.errorElement.classList.add(...this.errorElementClassList), this.errorElement.classList.add("dcf-d-none!"), this.fileInputElement.after(this.errorElement), this.fileInputElement.addEventListener("change", () => {
      if (this.errorElement.classList.add("dcf-d-none!"), this.validationFailedClass !== null && this.fileInputElement.classList.remove(this.validationFailedClass), this.fileInputElement.files.length !== 0 && this.fileInputElement.files[0].size > this.sizeLimit) {
        const filename = this.fileInputElement.files[0].name;
        this.fileInputElement.value = "", this.validationFailedClass !== null && this.fileInputElement.classList.add(this.validationFailedClass), this.fileInputElement.dispatchEvent(new CustomEvent("change")), this.errorElement.innerText = `The file '${filename}' is too large!`, this.errorElement.classList.remove("dcf-d-none!");
        return;
      }
    }), this.fileInputElement.classList.add("dcf-file-size-validator-initialized"), this.fileInputElement.removeAttribute("hidden"), this.fileInputElement.dispatchEvent(new CustomEvent(DCFFileSizeValidator.events("fileSizeValidatorReady"), {
      detail: {
        classInstance: this
      }
    }));
  }
  // The names of the events to be used easily
  static events(name) {
    const events = {
      fileSizeValidatorReady: "fileSizeValidatorReady"
    };
    return Object.freeze(events), name in events ? events[name] : void 0;
  }
  #parseSize(sizeValue) {
    if (typeof sizeValue != "string")
      throw new Error("parseSize: input must be a string");
    const input = sizeValue.trim(), match = /^(\d+(\.\d+)?)([a-zA-Z]+)?$/.exec(input);
    if (!match)
      throw new Error(`Invalid file size value: '${sizeValue}'`);
    const numericValue = parseFloat(match[1]), unitString = match[3] ? match[3].toLowerCase() : "", phpUnitMap = {
      kil: 1024,
      // acts as 'k'
      meg: 1024 ** 2,
      // acts as 'm'
      gig: 1024 ** 3,
      // acts as 'g'
      ter: 1024 ** 4,
      // acts as 't'
      pet: 1024 ** 5
      // acts as 'p'
    }, decimalUnitMap = {
      byt: 1,
      // 'b'
      kilb: 1e3,
      // 'kb'
      megb: 1e3 ** 2,
      // 'mb'
      gigb: 1e3 ** 3,
      // 'gb'
      terab: 1e3 ** 4,
      // 'tb'
      petab: 1e3 ** 5
      // 'pb'
    }, binaryUnitMap = {
      kib: 1024,
      mib: 1024 ** 2,
      gib: 1024 ** 3,
      tib: 1024 ** 4,
      pib: 1024 ** 5
    };
    let phpUnitKey = null;
    if (unitString === "k" ? phpUnitKey = "kil" : unitString === "m" ? phpUnitKey = "meg" : unitString === "g" ? phpUnitKey = "gig" : unitString === "t" ? phpUnitKey = "ter" : unitString === "p" && (phpUnitKey = "pet"), phpUnitKey !== null && phpUnitMap[phpUnitKey])
      return numericValue * phpUnitMap[phpUnitKey];
    let decimalKey = null;
    if (unitString === "b" ? decimalKey = "byt" : unitString === "kb" ? decimalKey = "kilb" : unitString === "mb" ? decimalKey = "megb" : unitString === "gb" ? decimalKey = "gigb" : unitString === "tb" ? decimalKey = "terab" : unitString === "pb" && (decimalKey = "petab"), decimalKey !== null && decimalUnitMap[decimalKey])
      return numericValue * decimalUnitMap[decimalKey];
    if (binaryUnitMap[unitString])
      return numericValue * binaryUnitMap[unitString];
    if (unitString === "")
      return numericValue;
    throw new Error(`Unrecognized size unit "${unitString}" in "${sizeValue}"`);
  }
  #formatSize(sizeInBytes) {
    return sizeInBytes >= 1073741824 ? `${Math.round(sizeInBytes / 1073741824)}<abbr title="Gigabytes">GB</abbr>` : sizeInBytes >= 1048576 ? `${Math.round(sizeInBytes / 1048576)}<abbr title="Megabytes">MB</abbr>` : sizeInBytes >= 1024 ? `${Math.round(sizeInBytes / 1024)}<abbr title="Kilobytes">KB</abbr>` : `${sizeInBytes} bytes`;
  }
}
class UNLFileSizeValidator extends DCFFileSizeValidator {
  static {
    __name(this, "UNLFileSizeValidator");
  }
  constructor(fileInput, options = {}) {
    "errorElementClassList" in options || (options.errorElementClassList = [
      "dcf-rounded",
      "unl-bg-scarlet",
      "unl-cream",
      "dcf-pt-4",
      "dcf-pb-4",
      "dcf-pl-2",
      "dcf-pr-2",
      "dcf-mt-3",
      "dcf-txt-center",
      "dcf-w-fit-content"
    ]), super(fileInput, options), window.UNL = window.UNL || {}, window.UNL.classes = window.UNL.classes || {}, window.UNL.classes[this.fileInputElement.getAttribute("id")] = this, this.fileInputElement.dispatchEvent(new Event("UNLClassReady"));
  }
}
export {
  UNLFileSizeValidator as default
};
//# sourceMappingURL=component.file-size-validator.js.map
