"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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BellContext = void 0;
const index_1 = require("../const/index");
const common_utils_1 = require("../utils/common-utils");
const DEFAULT_CALLER_BELL_FILEPATH = '/TUICallKit/static/phone_dialing.mp3';
const DEFAULT_CALLEE_BELL_FILEPATH = '/TUICallKit/static/phone_ringing.mp3';
class BellContext {
    constructor() {
        this._bellContext = null;
        this._isMuteBell = false;
        this._calleeBellFilePath = DEFAULT_CALLEE_BELL_FILEPATH;
        this._callRole = index_1.CallRole.UNKNOWN;
        this._callStatus = index_1.CallStatus.IDLE;
        // @ts-ignore
        this._bellContext = wx.createInnerAudioContext();
        this._bellContext.loop = true;
    }
    setBellSrc() {
        // @ts-ignore
        const fs = wx.getFileSystemManager();
        try {
            let playBellFilePath = DEFAULT_CALLER_BELL_FILEPATH;
            if (this._callRole === index_1.CallRole.CALLEE) {
                playBellFilePath = this._calleeBellFilePath || DEFAULT_CALLEE_BELL_FILEPATH;
            }
            fs.readFileSync(playBellFilePath, 'utf8', 0);
            this._bellContext.src = playBellFilePath;
        }
        catch (error) {
            console.warn(`${index_1.NAME.PREFIX}Failed to setBellSrc, ${error}`);
        }
    }
    setBellProperties(bellParams) {
        this._callRole = bellParams.callRole || this._callRole;
        this._callStatus = bellParams.callStatus || this._callStatus;
        this._calleeBellFilePath = bellParams.calleeBellFilePath || this._calleeBellFilePath;
        // undefined/false || isMuteBell => isMuteBell (不符合预期)
        this._isMuteBell = (0, common_utils_1.isUndefined)(bellParams.isMuteBell) ? this._isMuteBell : bellParams.isMuteBell;
    }
    play() {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                if (this._callStatus !== index_1.CallStatus.CALLING) {
                    return;
                }
                this.setBellSrc();
                if (this._callRole === index_1.CallRole.CALLEE && !this._isMuteBell) {
                    yield this._bellContext.play();
                }
                if (this._callRole === index_1.CallRole.CALLER) {
                    yield this._bellContext.play();
                }
            }
            catch (error) {
                console.warn(`${index_1.NAME.PREFIX}Failed to play audio file, ${error}`);
            }
        });
    }
    stop() {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                this._bellContext.stop();
            }
            catch (error) {
                console.warn(`${index_1.NAME.PREFIX}Failed to stop audio file, ${error}`);
            }
        });
    }
    setBellMute(enable) {
        return __awaiter(this, void 0, void 0, function* () {
            if (this._callStatus !== index_1.CallStatus.CALLING && this._callRole !== index_1.CallRole.CALLEE) {
                return;
            }
            if (enable) {
                yield this.stop();
            }
            else {
                yield this.play();
            }
        });
    }
    destroy() {
        try {
            this._isMuteBell = false;
            this._calleeBellFilePath = '';
            this._callRole = index_1.CallRole.UNKNOWN;
            this._callStatus = index_1.CallStatus.IDLE;
            this._bellContext.destroy();
            this._bellContext = null;
        }
        catch (error) {
            console.warn(`${index_1.NAME.PREFIX}Failed to destroy, ${error}`);
        }
    }
}
exports.BellContext = BellContext;
