"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const events_1 = require("events");
const Validate_1 = __importDefault(require("../Validate/Validate"));
class Proximity {
    messagePrefix;
    static MESSAGE_PREFIX = 'proximity';
    eventEmitter;
    lastDetected = false;
    /** @internal */
    constructor(messagePrefix) {
        this.messagePrefix = messagePrefix;
        this.eventEmitter = new events_1.EventEmitter();
    }
    /** @internal */
    onStateChange(callback) {
        (0, Validate_1.default)({ callback }).required().function();
        if (this.lastDetected) {
            callback(this.lastDetected);
        }
        this.eventEmitter.addListener('change', callback);
        return () => void this.eventEmitter.removeListener('change', callback);
    }
    /** @internal */
    handleMessageData(data) {
        switch (data.type) {
            case this.getMessage('change'):
                if (data.detected !== this.lastDetected) {
                    this.lastDetected = data.detected;
                    this.eventEmitter.emit('change', data.detected);
                }
                break;
            default:
        }
    }
    getMessage(name) {
        return this.messagePrefix + '.' + Proximity.MESSAGE_PREFIX + '.' + name;
    }
}
exports.default = Proximity;
//# sourceMappingURL=Proximity.js.map