/**
 * @license
 * Copyright 2025-2026 Open Home Foundation
 * SPDX-License-Identifier: Apache-2.0
 */
import { asObject, pickNumber, pickString } from "./attribute-shapes.js";
const CHIME_CLUSTER_ID = 1366;
const ATTR_INSTALLED_SOUNDS = 0;
const ATTR_SELECTED_CHIME = 1;
const ATTR_ENABLED = 2;
const ATTR_CLUSTER_REVISION = 65533;
function readAttr(node, endpoint, attrId) {
  return node.attributes[`${endpoint}/${CHIME_CLUSTER_ID}/${attrId}`];
}
function readSounds(node, endpoint) {
  const raw = readAttr(node, endpoint, ATTR_INSTALLED_SOUNDS);
  if (!Array.isArray(raw)) return [];
  const out = new Array();
  for (const item of raw) {
    const obj = asObject(item);
    if (!obj) continue;
    const id = pickNumber(obj, "chimeId") ?? pickNumber(obj, "0");
    const name = pickString(obj, "name") ?? pickString(obj, "1");
    if (id !== null && name !== null) out.push({ chimeId: id, name });
  }
  return out;
}
function readSelected(node, endpoint) {
  const v = readAttr(node, endpoint, ATTR_SELECTED_CHIME);
  return typeof v === "number" ? v : null;
}
function readEnabled(node, endpoint) {
  const v = readAttr(node, endpoint, ATTR_ENABLED);
  return v === true;
}
function readRevision(node, endpoint) {
  const v = readAttr(node, endpoint, ATTR_CLUSTER_REVISION);
  return typeof v === "number" ? v : 1;
}
async function setEnabled(client, nodeId, endpoint, enabled) {
  await client.writeAttribute(nodeId, `${endpoint}/${CHIME_CLUSTER_ID}/${ATTR_ENABLED}`, enabled);
}
async function setSelected(client, nodeId, endpoint, chimeId) {
  await client.writeAttribute(nodeId, `${endpoint}/${CHIME_CLUSTER_ID}/${ATTR_SELECTED_CHIME}`, chimeId);
}
async function play(client, nodeId, endpoint, chimeId) {
  const payload = chimeId !== void 0 ? { chimeID: chimeId } : {};
  await client.deviceCommand(nodeId, endpoint, CHIME_CLUSTER_ID, "PlayChimeSound", payload);
}
export {
  CHIME_CLUSTER_ID,
  play,
  readEnabled,
  readRevision,
  readSelected,
  readSounds,
  setEnabled,
  setSelected
};
//# sourceMappingURL=chime.js.map
