/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { Bytes, DerObject, hex, X520 } from "#general";
import { MATTER_EPOCH_OFFSET_S } from "#types";
import { InternalError } from "@matter/general";
const YEAR_S = 365 * 24 * 60 * 60;
function matterToJsDate(date) {
  return date === 0 ? X520.NON_WELL_DEFINED_DATE : new Date((date + MATTER_EPOCH_OFFSET_S) * 1e3);
}
function jsToMatterDate(date, addYears = 0) {
  return date.getTime() === X520.NON_WELL_DEFINED_DATE.getTime() ? 0 : Math.floor(date.getTime() / 1e3) - MATTER_EPOCH_OFFSET_S + addYears * YEAR_S;
}
function intTo16Chars(value) {
  const byteArray = new Uint8Array(8);
  const dataView = Bytes.dataViewOf(byteArray);
  dataView.setBigUint64(0, typeof value === "bigint" ? value : BigInt(value));
  return Bytes.toHex(byteArray).toUpperCase();
}
function uInt16To8Chars(value) {
  const byteArray = new Uint8Array(4);
  const dataView = Bytes.dataViewOf(byteArray);
  dataView.setUint32(0, value);
  return Bytes.toHex(byteArray).toUpperCase();
}
function uInt16To4Chars(value) {
  const byteArray = new Uint8Array(2);
  const dataView = Bytes.dataViewOf(byteArray);
  dataView.setUint16(0, value);
  return Bytes.toHex(byteArray).toUpperCase();
}
const MATTER_OID_TO_FIELD_MAP = {};
const GenericMatterObject = (oidBase, id, fieldName, valueConverter) => {
  const oidHex = `${oidBase}${hex.byte(id)}`;
  if (MATTER_OID_TO_FIELD_MAP[oidHex] !== void 0 && MATTER_OID_TO_FIELD_MAP[oidHex] !== fieldName) {
    throw new InternalError(
      `ASN.1 Matter OID mapping for ${oidHex} already exists with a different field name: "${MATTER_OID_TO_FIELD_MAP[oidHex]}" vs "${fieldName}"`
    );
  }
  MATTER_OID_TO_FIELD_MAP[oidHex] = fieldName;
  return (value) => [
    DerObject(oidHex, {
      value: (valueConverter ?? intTo16Chars)(value)
    })
  ];
};
const GenericMatterOpCertObject = (id, fieldName, valueConverter) => GenericMatterObject("2b0601040182a27c01", id, fieldName, valueConverter);
const GenericMatterAttCertObject = (id, fieldName, valueConverter) => GenericMatterObject("2b0601040182a27c02", id, fieldName, valueConverter);
const NodeId_Matter = GenericMatterOpCertObject(1, "nodeId");
const FirmwareSigningId_Matter = GenericMatterOpCertObject(2, "firmwareSigningId");
const IcacId_Matter = GenericMatterOpCertObject(3, "icacId");
const RcacId_Matter = GenericMatterOpCertObject(4, "rcacId");
const FabricId_Matter = GenericMatterOpCertObject(5, "fabricId");
const NocCat_Matter = GenericMatterOpCertObject(6, "caseAuthenticatedTag", uInt16To8Chars);
const VvsId_Matter = GenericMatterOpCertObject(7, "vvsId");
const VendorId_Matter = GenericMatterAttCertObject(1, "vendorId", uInt16To4Chars);
const ProductId_Matter = GenericMatterAttCertObject(2, "productId", uInt16To4Chars);
export {
  FabricId_Matter,
  FirmwareSigningId_Matter,
  IcacId_Matter,
  MATTER_OID_TO_FIELD_MAP,
  NocCat_Matter,
  NodeId_Matter,
  ProductId_Matter,
  RcacId_Matter,
  VendorId_Matter,
  VvsId_Matter,
  jsToMatterDate,
  matterToJsDate
};
//# sourceMappingURL=asn.js.map
