/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Base64 } from "#codec/Base64Codec.js";
import { DerCodec } from "#codec/DerCodec.js";
import { Bytes } from "#util/Bytes.js";
import { CertificateError } from "./CryptoError.js";
var Pem;
((Pem2) => {
  function encode(der, kind = "CERTIFICATE") {
    kind = kind.toUpperCase();
    let bytes;
    if (Bytes.isBytes(der)) {
      bytes = der;
    } else {
      bytes = DerCodec.encode(der);
    }
    const body = Base64.encode(Bytes.of(bytes)).match(/.{1,64}/g).join("\n");
    return `-----BEGIN ${kind}-----
${body}
-----END ${kind}-----`;
  }
  Pem2.encode = encode;
  function asDer(pemOrDer) {
    if (Bytes.isBytes(pemOrDer)) {
      if (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 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 CertificateError("No END line in PEM file");
    }
    const base64 = lines.slice(startPos + 1, endPos).join("").replace(/\s/g, "");
    try {
      return Base64.decode(base64);
    } catch (cause) {
      const error = new CertificateError(`Error in PEM base64 encoding`);
      error.cause = cause;
      throw error;
    }
  }
  Pem2.asDer = asDer;
  function decode(pemOrDer) {
    const bytes = asDer(pemOrDer);
    try {
      return DerCodec.decode(bytes);
    } catch (cause) {
      const error = new CertificateError(`Error in PEM DER encoding`);
      error.cause = cause;
      throw error;
    }
  }
  Pem2.decode = decode;
})(Pem || (Pem = {}));
export {
  Pem
};
//# sourceMappingURL=Pem.js.map
