/**
 * @license
 * Copyright 2022-2026 Matter.js Authors
 * SPDX-License-Identifier: Apache-2.0
 */
import { InternalError } from "@matter/general";
import { Write, WriteResult } from "@matter/protocol";
const attrCache = /* @__PURE__ */ new WeakMap();
function RemoteWriter(node, structure) {
  return async function writeRemote(request) {
    const attrWrites = Array();
    for (const { number, behaviorId, values } of request) {
      const cluster = structure.clusterFor(number, Number.parseInt(behaviorId));
      if (cluster === void 0) {
        throw new InternalError(`Cannot remote write to non-cluster behavior ${behaviorId}`);
      }
      const attrs = attrsFor(cluster);
      for (const id in values) {
        const attr = attrs[id];
        if (attr === void 0) {
          if (id.startsWith("__")) {
            continue;
          }
          throw new InternalError(`Cannot write unknown attribute ${id} for ${behaviorId}`);
        }
        attrWrites.push(
          Write.Attribute({
            endpoint: number,
            cluster,
            attributes: [attrs[id]],
            value: values[id]
          })
        );
      }
    }
    const write = Write(...attrWrites);
    WriteResult.assertSuccess(await node.interaction.write(write));
  };
}
function attrsFor(cluster) {
  let attrs = attrCache.get(cluster);
  if (attrs) {
    return attrs;
  }
  attrs = Object.fromEntries(Object.values(cluster.attributes).map((attr) => [attr.id, attr]));
  attrCache.set(cluster, attrs);
  return attrs;
}
export {
  RemoteWriter
};
//# sourceMappingURL=RemoteWriter.js.map
