"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        if (typeof b !== "function" && b !== null)
            throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __read = (this && this.__read) || function (o, n) {
    var m = typeof Symbol === "function" && o[Symbol.iterator];
    if (!m) return o;
    var i = m.call(o), r, ar = [], e;
    try {
        while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
    }
    catch (error) { e = { error: error }; }
    finally {
        try {
            if (r && !r.done && (m = i["return"])) m.call(i);
        }
        finally { if (e) throw e.error; }
    }
    return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
var meeting_electron_base_service_1 = require("./meeting_electron_base_service");
var electron_1 = require("electron");
var meeting_kit_1 = require("../meeting_kit");
var MODULE_NAME = 'NEMeetingRealtimeRecorderService';
var seqCount = 0;
var NEMeetingRealtimeRecorderService = /** @class */ (function (_super) {
    __extends(NEMeetingRealtimeRecorderService, _super);
    function NEMeetingRealtimeRecorderService(_win) {
        var _this = _super.call(this, _win) || this;
        _this._listeners = [];
        _this._addListening();
        return _this;
    }
    NEMeetingRealtimeRecorderService.prototype.startRealtimeRecorder = function (param, opts) {
        var _this = this;
        var _a, _b;
        console.log('startRealTimeRecorder', param, opts);
        var functionName = 'startRealtimeRecorder';
        var seqId = this._generateSeqId(functionName);
        this._win.webContents.send(meeting_kit_1.BUNDLE_NAME, {
            module: MODULE_NAME,
            method: functionName,
            args: [param, opts],
            seqId: seqId,
        });
        (_b = (_a = this._win).initMainRecordWindowSize) === null || _b === void 0 ? void 0 : _b.call(_a);
        return this._IpcMainListener(seqId).then(function (res) {
            var _a, _b;
            (_b = (_a = _this._win).initMainRecordWindowSize) === null || _b === void 0 ? void 0 : _b.call(_a);
            _this._win.show();
            _this._win.inMeeting = true;
            return res;
        });
    };
    NEMeetingRealtimeRecorderService.prototype._generateSeqId = function (functionName) {
        seqCount++;
        return "".concat(meeting_kit_1.BUNDLE_NAME, "::").concat(MODULE_NAME, "::").concat(functionName, "::").concat(seqCount);
    };
    NEMeetingRealtimeRecorderService.prototype.stopRealtimeRecorder = function () {
        var functionName = 'stopRealtimeRecorder';
        var seqId = this._generateSeqId(functionName);
        this._win.webContents.send(meeting_kit_1.BUNDLE_NAME, {
            module: MODULE_NAME,
            method: functionName,
            args: [],
            seqId: seqId,
        });
        return this._IpcMainListener(seqId);
    };
    NEMeetingRealtimeRecorderService.prototype.addMeetingRealtimeRecorderStatusListener = function (listener) {
        this._listeners.push(listener);
    };
    NEMeetingRealtimeRecorderService.prototype.removeMeetingRealtimeRecorderStatusListener = function (listener) {
        if (!listener) {
            this._listeners = [];
        }
        this._listeners = this._listeners.filter(function (l) { return l !== listener; });
    };
    NEMeetingRealtimeRecorderService.prototype._addListening = function () {
        var _this = this;
        var channel = "NEMeetingKitListener::".concat(MODULE_NAME);
        electron_1.ipcMain.removeAllListeners(channel);
        electron_1.ipcMain.removeHandler(channel);
        electron_1.ipcMain.on(channel, function (_, data) {
            var _a;
            var module = data.module, event = data.event, payload = data.payload;
            if (module !== MODULE_NAME) {
                return;
            }
            if (event === 'onMeetingRealtimeRecorderStatusChanged') {
                // fix 重复调用stopRealtimeRecorder偶现报错问题
                (_a = _this._listeners) === null || _a === void 0 ? void 0 : _a.forEach(function (l) {
                    var _a;
                    (_a = l === null || l === void 0 ? void 0 : l[event]) === null || _a === void 0 ? void 0 : _a.call.apply(_a, __spreadArray([l], __read(payload), false));
                });
            }
        });
    };
    return NEMeetingRealtimeRecorderService;
}(meeting_electron_base_service_1.default));
exports.default = NEMeetingRealtimeRecorderService;