"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 PropertyResource_exports = {};
__export(PropertyResource_exports, {
  PropertyResource: () => PropertyResource
});
module.exports = __toCommonJS(PropertyResource_exports);
var import_RootSupervisor = require("#behavior/supervision/RootSupervisor.js");
var import_general = require("@matter/general");
var import_model = require("@matter/model");
var import_types = require("@matter/types");
var import_ApiResource = require("../ApiResource.js");
var import_Envelope = require("../Envelope.js");
/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
class PropertyResource extends import_ApiResource.ApiResource {
  id;
  supervisor;
  dataModelPath;
  get valueKind() {
    if (this.schema.tag === "attribute") {
      return "attribute";
    }
    return "field";
  }
  constructor(parent, id, supervisor, path) {
    super(parent);
    this.id = id;
    this.supervisor = supervisor;
    this.dataModelPath = path;
  }
  get schema() {
    return this.supervisor.schema;
  }
  get value() {
    return this.parent?.value?.[this.id];
  }
  write(request) {
    const requestEnv = new import_Envelope.Envelope({ supervisor: this.supervisor, ...request });
    requestEnv.validate();
    this.#target[this.id] = requestEnv.js;
  }
  patch(request) {
    request = new import_Envelope.Envelope({ supervisor: this.supervisor, ...request });
    request.validate();
    this.#targetSupervisor.patch({ [this.id]: request.js }, this.#target, this.dataModelPath);
  }
  add(request) {
    const struct = this.#target;
    if (!Array.isArray(struct)) {
      throw new import_general.NotImplementedError();
    }
    request = new import_Envelope.Envelope({ supervisor: this.supervisor, ...request });
    struct.push(request.js);
  }
  delete() {
    const struct = this.#target;
    if (Array.isArray(struct)) {
      struct.splice(this.id, 1);
    } else {
      this.#target[this.id] = void 0;
    }
  }
  async childFor(id) {
    let mySchema;
    switch (this.schema.effectiveMetatype) {
      case import_model.Metatype.object:
        mySchema = this.schema.conformant.properties.for(id);
        break;
      case import_model.Metatype.array:
        if (!id.match(/^\d+$/)) {
          mySchema = void 0;
        } else {
          mySchema = this.schema.conformant.properties.for("entry");
        }
        break;
      default:
        throw new import_general.NotImplementedError();
    }
    if (!mySchema) {
      throw new import_types.StatusResponse.NotFoundError();
    }
    const myCollection = this.#target[this.id];
    if (!myCollection) {
      return;
    }
    return new PropertyResource(this, id, this.supervisorFor(mySchema), this.dataModelPath.at(id));
  }
  get #target() {
    const collection = this.parent?.value;
    if (!collection || typeof collection !== "object") {
      throw new import_general.InternalError("Value of property item has no collection");
    }
    return collection;
  }
  get #targetSupervisor() {
    const supervisor = import_RootSupervisor.RootSupervisor.for(this.parent?.schema);
    if (!supervisor) {
      throw new import_general.InternalError("No supervisor for parent collection");
    }
    return supervisor;
  }
}
//# sourceMappingURL=PropertyResource.js.map
