"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const WebSocket_1 = __importDefault(require("./WebSocket"));
const eventEmitter_1 = __importDefault(require("./../utils/eventEmitter"));
const enum_1 = require("../ts/enum");
class CoolKitWs {
    init(config) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                if ((config.userAgent === 'app' || config.userAgent === 'pc_ewelink') && (!config.at || !config.apikey || !config.appid || !config.region)) {
                    throw new Error('WS:app端长连接缺少必须参数！');
                }
                if (config.userAgent === 'device' && (!config.region || !config.apikey || !config.deviceid)) {
                    throw new Error('WS:设备端长连接缺少必须参数！');
                }
                const newWs = new WebSocket_1.default(config);
                const result = yield newWs.init();
                CoolKitWs.ws = newWs;
                return result;
            }
            catch (error) {
                throw new Error(`WS:长连接连接出错, ${error}`);
            }
        });
    }
    close() {
        const ws = this.getWs();
        ws.close(true);
    }
    isWsExist() {
        if (CoolKitWs.ws) {
            WebSocket_1.default.connectConfig.debug && console.log(`WS:长连接已存在，状态为${CoolKitWs.ws.wsState === 'CLOSED' ? '已关闭' : '开启中'}`);
            return CoolKitWs.ws.wsState === 'CLOSED' ? false : true;
        }
        return false;
    }
    sendMessage(params) {
        const ws = this.getWs();
        ws.send(params);
    }
    updateThing(deviceConfig) {
        return __awaiter(this, void 0, void 0, function* () {
            const ws = this.getWs();
            const { deviceid, ownerApikey, params } = deviceConfig;
            if (!deviceid || !ownerApikey || !params) {
                throw new Error('WS:参数不能为空');
            }
            const { sequence } = yield ws.sendThing(deviceConfig, 'update');
            return yield this.getMessage(sequence, deviceid);
        });
    }
    queryThing(queryConfig) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!CoolKitWs.ws || (CoolKitWs.ws && CoolKitWs.ws.wsState === 'CLOSED')) {
                return {
                    error: 604,
                    msg: 'Websocket is closed.',
                };
            }
            const { deviceid, ownerApikey, params } = queryConfig;
            if (!deviceid || !ownerApikey || !params) {
                throw new Error('WS:参数不能为空');
            }
            const { sequence } = yield CoolKitWs.ws.sendThing(queryConfig, 'query');
            return yield this.getMessage(sequence, deviceid);
        });
    }
    upgradeThing(upgradeConfig) {
        return __awaiter(this, void 0, void 0, function* () {
            if (!CoolKitWs.ws || (CoolKitWs.ws && CoolKitWs.ws.wsState === 'CLOSED')) {
                return {
                    error: 604,
                    msg: 'Websocket is closed.',
                };
            }
            const { deviceid, ownerApikey, params } = upgradeConfig;
            if (!deviceid || !ownerApikey || !params) {
                throw new Error('WS:参数不能为空');
            }
            const { sequence } = yield CoolKitWs.ws.sendThing(upgradeConfig, 'upgrade');
            return yield this.getMessage(sequence, deviceid);
        });
    }
    on(method, callback) {
        return __awaiter(this, void 0, void 0, function* () {
            const ws = this.getWs();
            if (WebSocket_1.default.ws && ws) {
                if (method === 'reconnect') {
                    eventEmitter_1.default.on(`${enum_1.EOpenEventType.RECONNECT}`, (errorMsg) => callback(errorMsg));
                    return;
                }
                eventEmitter_1.default.on(method, callback);
            }
        });
    }
    getMessage(sequence, deviceid) {
        return new Promise((resolve) => {
            const { reqTimeout = 15000 } = WebSocket_1.default.connectConfig;
            setTimeout(() => {
                resolve({
                    error: 604,
                    msg: 'ws request is timeout.',
                });
            }, reqTimeout);
            eventEmitter_1.default.once(`${enum_1.EEventType.DEVICE_MSG}${sequence}${deviceid}`, (data) => resolve(data));
        });
    }
    getWs() {
        if (CoolKitWs.ws) {
            return CoolKitWs.ws;
        }
        throw new Error('WS:请使用 init 方法初始化长连接后再调用本方法！');
    }
}
exports.default = CoolKitWs;
CoolKitWs.ws = null;
