"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const _1 = require("../../../../../Class/$");
const Functional_1 = require("../../../../../Class/Functional");
const _ids_1 = require("../../../../_ids");
class BluetoothCharacteristic_ extends Functional_1.Functional {
    constructor(system) {
        super({
            i: ['service', 'uuid'],
            o: ['charac'],
        }, {
            input: {
                service: {
                    ref: true,
                },
            },
            output: {
                charac: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_BLUETOOTH_CHARACTERISTIC);
        this._started = false;
        this.addListener('listen', ({ event }) => {
            if (event === 'write') {
                this.startNotifications();
            }
        });
        this.addListener('unlisten', ({ event }) => {
            if (event === 'write') {
                this.stopNotification();
            }
        });
    }
    async f({ service, uuid }, done) {
        let characteristic;
        try {
            characteristic = await service.getCharacteristic(uuid);
        }
        catch (err) {
            done(undefined, err.message.toLowerCase());
            return;
        }
        this._characteristic = characteristic;
        const charac = new (class _BluetoothDevice extends _1.$ {
            constructor() {
                super(...arguments);
                this.__ = ['BC'];
            }
            async read() {
                const dataView = await characteristic.readValue();
                return dataView.getUint8(0).toString();
            }
            async write(data) {
                const charCodeArray = data.split('').map((c) => c.charCodeAt(0));
                const buffer = Uint8Array.from(charCodeArray);
                await characteristic.writeValue(buffer);
                return;
            }
        })(this.__system);
        done({ charac });
    }
    d() {
        this.stopNotification();
        this._characteristic = undefined;
    }
    async startNotifications() {
        if (this._started) {
            return;
        }
        const characteristic = this._characteristic;
        this._started = true;
        characteristic.startNotifications();
        characteristic.addEventListener('characteristicvaluechanged', (event) => {
            const dataView = event.target.value;
            const { byteLength } = dataView;
            let value = '';
            for (let i = 0; i < byteLength; i++) {
                value += String.fromCharCode(dataView.getUint8(i));
            }
            event.stopImmediatePropagation();
            this.emit('characteristicvaluechanged', value, true);
        });
    }
    stopNotification() {
        if (this._started) {
            return;
        }
        this._started = false;
        this._characteristic.stopNotifications();
    }
}
exports.default = BluetoothCharacteristic_;
//# sourceMappingURL=index.js.map