"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GlobalsService = void 0;
const inversify_1 = require("inversify");
const session_js_1 = require("../session.js");
const util = __importStar(require("../util/index.js"));
let GlobalsService = class GlobalsService {
    constructor() {
        this.values = [];
    }
    async onSessionStart() {
        await this.clean();
    }
    async onSessionFinish() {
        await this.clean();
    }
    async clean() {
        this.values = [];
    }
    getKeys() {
        return this.values.map(_ => _.key);
    }
    getGlobal(key, optional = false) {
        util.assertScript(key, 'key is required to access globals');
        const globl = this.values.find(_ => _.key === key) || { value: null };
        const value = globl.value == null ? null : globl.value;
        util.assertPlayback(value != null || optional, `Requested global ${key} is null. Use optional: true if this is intended.`);
        return value;
    }
    setGlobal(key, value) {
        const existing = this.values.find(_ => _.key === key);
        if (existing) {
            existing.value = value;
        }
        else {
            this.values.push({ key, value });
        }
    }
    appendGlobal(key, values) {
        const existing = this.values.find(_ => _.key === key);
        if (existing) {
            const array = existing.value == null ? [] : existing.value;
            const type = util.getType(array);
            util.assertPlayback(type === 'array', `Expected global to contain array, instead got ${type}`);
            existing.value = array.concat(values);
        }
        else {
            this.values.push({ key, value: values });
        }
    }
    removeGlobal(key) {
        this.values = this.values.filter(_ => _.key !== key);
    }
};
GlobalsService = __decorate([
    (0, inversify_1.injectable)(),
    (0, session_js_1.SessionHandler)()
], GlobalsService);
exports.GlobalsService = GlobalsService;
//# sourceMappingURL=globals.js.map