"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.definitions = void 0;
const zigbee_herdsman_1 = require("zigbee-herdsman");
const m = __importStar(require("../lib/modernExtend"));
const utils = __importStar(require("../lib/utils"));
const AIR_QUALITY_CLUSTER = "aircubeAirQuality";
/**
 * The AirCube ships as two hardware variants that enumerate with the SAME
 * `modelID` ("AirCube") from a single firmware image:
 *   - Base: ENS210 (temp/RH) + ENS16x (eCO2, tVOC, VOC level).
 *   - Pro : SCD41 (true CO2, msCO2 cluster) + VCNL4040 (ambient light,
 *           msIlluminanceMeasurement cluster) on top of the ENS16x set.
 * The Pro-only clusters are only added to the endpoint when the hardware is a
 * Pro, so CO2 and illuminance are exposed only when the device actually
 * implements the cluster. Both the exposes and the configure are gated: an
 * ungated m.co2()/m.illuminance() configure calls getEndpointsWithCluster(),
 * which throws on a Base unit that has no such cluster. Pattern mirrors
 * third_reality.ts conditionalPressure().
 */
function whenClusterPresent(cluster, extend) {
    const present = (device) => {
        if (utils.isDummyDevice(device))
            return true; // documentation generation: always show
        return device.endpoints?.some((endpoint) => endpoint.supportsInputCluster(cluster)) ?? false;
    };
    const exposeFn = (device, options) => {
        if (!present(device))
            return [];
        const result = [];
        for (const item of extend.exposes ?? []) {
            if (typeof item === "function")
                result.push(...item(device, options));
            else
                result.push(item);
        }
        return result;
    };
    return {
        ...extend,
        exposes: [exposeFn],
        configure: (extend.configure ?? []).map((configureFn) => {
            return async (device, coordinatorEndpoint, definition) => {
                if (present(device))
                    await configureFn(device, coordinatorEndpoint, definition);
            };
        }),
        isModernExtend: true,
    };
}
exports.definitions = [
    {
        zigbeeModel: ["AirCube"],
        model: "AirCube",
        vendor: "StuckAtPrototype",
        description: "AirCube air quality monitor",
        extend: [
            m.deviceAddCustomCluster(AIR_QUALITY_CLUSTER, {
                name: AIR_QUALITY_CLUSTER,
                ID: 0xfc01,
                attributes: {
                    eco2: { name: "eco2", ID: 0x0000, type: zigbee_herdsman_1.Zcl.DataType.UINT16 },
                    etvoc: { name: "etvoc", ID: 0x0001, type: zigbee_herdsman_1.Zcl.DataType.UINT16 },
                    aqi: { name: "aqi", ID: 0x0002, type: zigbee_herdsman_1.Zcl.DataType.UINT16 },
                },
                commands: {},
                commandsResponse: {},
            }),
            m.temperature({ reporting: { min: "10_SECONDS", max: "1_MINUTE", change: 50 } }),
            m.humidity({ reporting: { min: "10_SECONDS", max: "1_MINUTE", change: 100 } }),
            m.numeric({
                name: "eco2",
                cluster: AIR_QUALITY_CLUSTER,
                attribute: { ID: 0x0000, type: zigbee_herdsman_1.Zcl.DataType.UINT16 },
                label: "Equivalent CO2",
                description: "Equivalent CO2 estimated from VOC (ENS16x)",
                unit: "ppm",
                access: "STATE_GET",
                valueMin: 400,
                valueMax: 8192,
                reporting: { min: "10_SECONDS", max: "1_MINUTE", change: 50 },
            }),
            m.numeric({
                name: "voc",
                cluster: AIR_QUALITY_CLUSTER,
                attribute: { ID: 0x0001, type: zigbee_herdsman_1.Zcl.DataType.UINT16 },
                label: "VOC parts",
                description: "Equivalent total VOC (tVOC)",
                unit: "ppb",
                access: "STATE_GET",
                valueMin: 0,
                valueMax: 65535,
                reporting: { min: "10_SECONDS", max: "1_MINUTE", change: 10 },
            }),
            m.numeric({
                name: "aqi",
                cluster: AIR_QUALITY_CLUSTER,
                attribute: { ID: 0x0002, type: zigbee_herdsman_1.Zcl.DataType.UINT16 },
                label: "VOC level",
                description: "TVOC-derived VOC level (0-500)",
                access: "STATE_GET",
                valueMin: 0,
                valueMax: 500,
                reporting: { min: "10_SECONDS", max: "1_MINUTE", change: 5 },
            }),
            m.numeric({
                name: "brightness",
                cluster: "genAnalogOutput",
                attribute: "presentValue",
                label: "Brightness",
                description: "LED brightness",
                unit: "%",
                access: "ALL",
                valueMin: 0,
                valueMax: 100,
                valueStep: 1,
                precision: 0,
                reporting: { min: "10_SECONDS", max: "1_MINUTE", change: 5 },
            }),
            whenClusterPresent("msCO2", m.co2()),
            whenClusterPresent("msIlluminanceMeasurement", m.illuminance()),
            m.identify(),
        ],
    },
];
//# sourceMappingURL=stuckatprototype.js.map