"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;
    }
    onStatusChange(onStatusChange) {
        const devices = this.commissioningController.getDevices();
        console.log('devices', devices.length);
        if (devices[0]) {
            const onOff = devices[0].getClusterClient(cluster_1.OnOffCluster);
            if (onOff !== undefined) {
                onOff.getOnOffAttribute().then((value) => {
                    console.log('initial onOffStatus', value);
                    onStatusChange(value);
                });
                onOff.addOnOffAttributeListener((value) => {
                    console.log('updated onOffStatus', value);
                    onStatusChange(value);
                });
            }
        }
    }
}
exports.MatterController = MatterController;
