"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const matter_device_1 = require("../modules/matter-device");
function default_1(RED) {
    RED.httpAdmin.route('/node-red-matter/pairingcode').get(function (req, res) {
        const deviceId = req.query['device-id'];
        const node = RED.nodes.getNode(deviceId);
        if (node == null) {
            res.status(404).send('Device not found');
            return;
        }
        const message = {
            commissioned: node.commissioned,
            qrcode: node.qrcode,
            manualPairingCode: node.manualPairingCode,
        };
        res.json(message);
    });
    function MatterDeviceNode(config) {
        const node = this;
        RED.nodes.createNode(node, config);
        const server = RED.nodes.getNode(config.server);
        const matterDevice = new matter_device_1.MatterOnOffDevice(config.devicetype, Number(config.port), node.id, Number(config.discriminator), Number(config.productid ?? 0x8000));
        matterDevice
            .start({
            onStatusChange: (status) => {
                const message = {
                    type: config.devicetype,
                    status,
                };
                node.emit('status_change', message);
            },
        })
            .then(async (commissioningServer) => {
            // Register device to Matter server
            if (server == null) {
                node.warn(`Matter server ${config.server} not found`);
                return;
            }
            server.emit('add_commissioning_server', node.id, commissioningServer);
            await server.serverPromise;
            const message = await matterDevice.getPairingData();
            // Store pairing code
            node.qrcode = message.qrCode;
            node.manualPairingCode = message.manualPairingCode;
            node.commissioned = message.commissioned;
            // Handle device updates
            if (matterDevice.device == null) {
                return;
            }
            switch (config.devicetype) {
                case matter_device_1.DeviceType.OnOffLightDevice:
                case matter_device_1.DeviceType.OnOffPluginUnitDevice:
                    matterDevice.device.isOn().then((status) => {
                        const updateStatusMessage = {
                            type: config.devicetype,
                            status,
                        };
                        node.emit('status_change', updateStatusMessage);
                    });
                    break;
                default:
                    node.warn(`Unknown device type ${config.devicetype}`);
            }
        });
        // Receive status change requests from matter-device-control nodes
        node.addListener('change_status', (status) => {
            if (matterDevice.device == null) {
                return;
            }
            switch (config.devicetype) {
                case matter_device_1.DeviceType.OnOffLightDevice:
                case matter_device_1.DeviceType.OnOffPluginUnitDevice:
                    if (typeof status !== 'boolean') {
                        node.warn(`Invalid status ${status} for device type ${config.devicetype}`);
                        return;
                    }
                    matterDevice.device.onOff(status);
                    break;
                default:
                    node.warn(`Unknown device type ${config.devicetype}`);
            }
        });
    }
    RED.nodes.registerType('matter-device', MatterDeviceNode);
}
exports.default = default_1;
