// src/formatter/abstract-formatter.ts
var AbstractFormatter = class {
};

// src/errors/invalid-argument-error.ts
var InvalidArgumentError = class extends Error {
};

// src/utils/php/base64.ts
function base64Decode(base64Input) {
  return Buffer.from(base64Input, "base64");
}

// src/formatter/base64.ts
var _Base64 = class _Base64 extends AbstractFormatter {
  /**
   * @param variant Compatible with BASE64_VARIANT_*
   * @param strict Whether to throw exception on decoding errors
   * @throws Error
   */
  constructor(variant = _Base64.BASE64_VARIANT_URLSAFE_NO_PADDING, strict = false) {
    super();
    if (![
      _Base64.BASE64_VARIANT_ORIGINAL,
      _Base64.BASE64_VARIANT_ORIGINAL_NO_PADDING,
      _Base64.BASE64_VARIANT_URLSAFE,
      _Base64.BASE64_VARIANT_URLSAFE_NO_PADDING
    ].includes(variant)) {
      throw new InvalidArgumentError("Invalid base64 variant");
    }
    this.variant = variant;
    this.strict = strict;
  }
  /**
   * Encodes a raw binary string with base64
   * @param value
   * @return string
   * @throws Error
   */
  format(value) {
    switch (this.variant) {
      case _Base64.BASE64_VARIANT_ORIGINAL:
        return Buffer.from(value).toString("base64");
      case _Base64.BASE64_VARIANT_ORIGINAL_NO_PADDING:
        return Buffer.from(value).toString("base64").replace(/=+$/, "");
      case _Base64.BASE64_VARIANT_URLSAFE:
        return Buffer.from(value).toString("base64").replace(/\+/g, "-").replace(/\//g, "_");
      case _Base64.BASE64_VARIANT_URLSAFE_NO_PADDING:
        return Buffer.from(value).toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
      default:
        throw new InvalidArgumentError("Invalid base64 variant");
    }
  }
  /**
   * Decodes a base64-encoded string into raw binary
   * @param value
   * @return string
   * @throws Error When strict mode is enabled, an exception is thrown in case of unrecognized character
   */
  parse(value) {
    let binary = base64Decode(value);
    if (this.strict && !binary) {
      throw new InvalidArgumentError("Not a valid base64-encoded value");
    }
    return binary;
  }
};
_Base64.BASE64_VARIANT_ORIGINAL = 1;
_Base64.BASE64_VARIANT_ORIGINAL_NO_PADDING = 3;
_Base64.BASE64_VARIANT_URLSAFE = 5;
_Base64.BASE64_VARIANT_URLSAFE_NO_PADDING = 7;
var Base64 = _Base64;
export {
  AbstractFormatter,
  Base64
};
//# sourceMappingURL=index.mjs.map