"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Pem_exports = {};
__export(Pem_exports, {
  Pem: () => Pem
});
module.exports = __toCommonJS(Pem_exports);
var import_Base64Codec = require("#codec/Base64Codec.js");
var import_DerCodec = require("#codec/DerCodec.js");
var import_Bytes = require("#util/Bytes.js");
var import_CryptoError = require("./CryptoError.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
var Pem;
((Pem2) => {
  function encode(der, kind = "CERTIFICATE") {
    kind = kind.toUpperCase();
    let bytes;
    if (import_Bytes.Bytes.isBytes(der)) {
      bytes = der;
    } else {
      bytes = import_DerCodec.DerCodec.encode(der);
    }
    const body = import_Base64Codec.Base64.encode(import_Bytes.Bytes.of(bytes)).match(/.{1,64}/g).join("\n");
    return `-----BEGIN ${kind}-----
${body}
-----END ${kind}-----`;
  }
  Pem2.encode = encode;
  function asDer(pemOrDer) {
    if (import_Bytes.Bytes.isBytes(pemOrDer)) {
      if (import_Bytes.Bytes.of(pemOrDer)[0] === "-".charCodeAt(0)) {
        pemOrDer = new TextDecoder().decode(pemOrDer);
      } else {
        return pemOrDer;
      }
    }
    const lines = pemOrDer.split("\n");
    let startPos = 0;
    while (startPos < lines.length && !lines[startPos].startsWith("-----BEGIN ")) {
      startPos++;
    }
    if (startPos >= lines.length) {
      throw new import_CryptoError.CertificateError("No BEGIN line in PEM file");
    }
    let endPos = startPos + 1;
    while (endPos < lines.length && !lines[endPos].startsWith("-----END ")) {
      endPos++;
    }
    if (endPos >= lines.length) {
      throw new import_CryptoError.CertificateError("No END line in PEM file");
    }
    const base64 = lines.slice(startPos + 1, endPos).join("").replace(/\s/g, "");
    try {
      return import_Base64Codec.Base64.decode(base64);
    } catch (cause) {
      const error = new import_CryptoError.CertificateError(`Error in PEM base64 encoding`);
      error.cause = cause;
      throw error;
    }
  }
  Pem2.asDer = asDer;
  function decode(pemOrDer) {
    const bytes = asDer(pemOrDer);
    try {
      return import_DerCodec.DerCodec.decode(bytes);
    } catch (cause) {
      const error = new import_CryptoError.CertificateError(`Error in PEM DER encoding`);
      error.cause = cause;
      throw error;
    }
  }
  Pem2.decode = decode;
})(Pem || (Pem = {}));
//# sourceMappingURL=Pem.js.map
