"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 Envelope_exports = {};
__export(Envelope_exports, {
  Envelope: () => Envelope
});
module.exports = __toCommonJS(Envelope_exports);
var import_LocalActorContext = require("#behavior/context/server/LocalActorContext.js");
var import_general = require("@matter/general");
var import_model = require("@matter/model");
var import_types = require("@matter/types");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class Envelope {
  #supervisor;
  #js;
  #json;
  #tlv;
  constructor({ supervisor, js, json, tlv }) {
    this.#supervisor = supervisor;
    this.#js = js;
    this.#json = json;
    this.#tlv = tlv;
  }
  /**
   * Validate against the schema.  Casts to appropriate types if necessary.
   */
  validate(path) {
    if (!path) {
      path = new import_model.DataModelPath(this.#supervisor.schema.path);
    }
    this.#js = this.#supervisor.cast(this.#js);
    this.#supervisor.validate?.(this.js, import_LocalActorContext.LocalActorContext.ReadOnly, { path });
  }
  /**
   * Convert a {@link js} value to {@link JSON}.
   *
   * This acts as a deep copy and optimizes JSON access.  If TLV becomes a priority then we can make this conversion
   * configurable.
   */
  convertToJson() {
    if (this.#js !== void 0) {
      void this.json;
    }
    this.#js = void 0;
  }
  /**
   * Native JS format.
   */
  get js() {
    if (this.#js === void 0) {
      if (this.#json) {
        try {
          this.#js = JSON.parse(this.#json);
        } catch (e) {
          if (e instanceof SyntaxError) {
            throw new import_types.StatusResponse.FailureError(`Unparseable JSON: ${e.message}`);
          }
          throw e;
        }
      } else if (this.#tlv) {
        this.#js = (0, import_types.TlvOfModel)(this.#supervisor.schema).decode(this.#tlv);
      } else {
        this.#js = null;
      }
    }
    return this.#js;
  }
  /**
   * JSON format.
   */
  get json() {
    if (this.#json === void 0) {
      this.#json = (0, import_general.asJson)(this.js);
    }
    return this.#json;
  }
  /**
   * Serialized TLV format.
   */
  get tlv() {
    if (this.#tlv === void 0) {
      this.#tlv = (0, import_types.TlvOfModel)(this.#supervisor.schema).encode(this.#js);
    }
    return this.#tlv;
  }
}
//# sourceMappingURL=Envelope.js.map
