"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MatterDeviceStatusNode = void 0;
const matter_device_1 = require("../modules/matter-device");
function MatterDeviceStatusNode(RED) {
    return function (config) {
        const node = this;
        RED.nodes.createNode(node, config);
        const matterDeviceNode = RED.nodes.getNode(config.device);
        node.status({
            fill: 'grey',
            shape: 'dot',
            text: 'unknown',
        });
        if (matterDeviceNode == null) {
            node.error(`Associated matter-device node (${config.device}) not found`);
            return;
        }
        matterDeviceNode.addListener('status_change', (status) => {
            node.send({
                payload: status,
            });
            if (status.type === matter_device_1.DeviceType.OnOffLightDevice ||
                status.type === matter_device_1.DeviceType.OnOffPluginUnitDevice) {
                node.status({
                    fill: status.status
                        ? 'green'
                        : status.status === false
                            ? 'red'
                            : 'grey',
                    shape: 'dot',
                    text: status.status
                        ? 'on'
                        : status.status === false
                            ? 'off'
                            : 'unknown',
                });
            }
        });
    };
}
exports.MatterDeviceStatusNode = MatterDeviceStatusNode;
function default_1(RED) {
    RED.nodes.registerType('matter-device-status', MatterDeviceStatusNode(RED));
}
exports.default = default_1;
