"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MatterController = void 0;
const matter_node_js_1 = require("@project-chip/matter-node.js");
const cluster_1 = require("@project-chip/matter-node.js/cluster");
const schema_1 = require("@project-chip/matter-node.js/schema");
const util_1 = require("@project-chip/matter-node.js/util");
class MatterController {
    commissioningController;
    constructor(pairingCode, ip, port) {
        const pairingCodeCodec = schema_1.ManualPairingCodeCodec.decode(pairingCode);
        const shortDiscriminator = pairingCodeCodec.shortDiscriminator;
        const passcode = pairingCodeCodec.passcode;
        const commissioningOptions = {
            regulatoryLocation: 2 /* GeneralCommissioning.RegulatoryLocationType.IndoorOutdoor */,
            regulatoryCountryCode: 'XX',
        };
        const commissioningController = new matter_node_js_1.CommissioningController({
            serverAddress: ip != null && port != null ? { ip, port, type: 'udp' } : undefined,
            longDiscriminator: undefined,
            shortDiscriminator,
            passcode,
            delayedPairing: true,
            commissioningOptions,
            subscribeAllAttributes: true,
        });
        (0, util_1.logEndpoint)(commissioningController.getRootEndpoint());
        this.commissioningController = commissioningController;
    }
    addStatusChangeListener(onStatusChange) {
        const devices = this.commissioningController.getDevices();
        const device = devices[0];
        if (device == null) {
            console.error('No devices found');
            return;
        }
        // if the device has child endpoints, subscribe the on/off cluster on each of them
        if (device.getChildEndpoints().length > 0) {
            device.getChildEndpoints().forEach((endpoint) => {
                const onOff = endpoint.getClusterClient(cluster_1.OnOffCluster);
                if (onOff !== undefined) {
                    onOff.getOnOffAttribute().then((value) => {
                        onStatusChange({
                            name: endpoint.name,
                            status: value,
                            id: endpoint.id,
                        });
                    });
                    onOff.addOnOffAttributeListener((value) => {
                        onStatusChange({
                            name: endpoint.name,
                            status: value,
                            id: endpoint.id,
                        });
                    });
                }
            });
        }
        // Also subscribe the on/off cluster on the root endpoint if it exists
        const onOff = device.getClusterClient(cluster_1.OnOffCluster);
        if (onOff !== undefined) {
            onOff.getOnOffAttribute().then((value) => {
                onStatusChange({ name: device.name, status: value, id: device.id });
            });
            onOff.addOnOffAttributeListener((value) => {
                onStatusChange({ name: device.name, status: value, id: device.id });
            });
        }
    }
}
exports.MatterController = MatterController;
