"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 _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sessionClient = void 0;
exports.sessionClient = (_a = class Session {
        static replaceNameSession(nameSession) {
            return nameSession.replace(/[^0-9a-zA-Zs]/g, '');
        }
        // Use this function to add a new session!
        // nameSession = client name, example: test, job ... etc.
        static newSession(nameSession) {
            return __awaiter(this, void 0, void 0, function* () {
                // checks if the clinet name exists in the session
                nameSession = this.replaceNameSession(nameSession);
                const check = this.session.filter((e) => e.name === nameSession);
                if (!check.length) {
                    // creating a new session!
                    this.session.push({
                        name: nameSession,
                    });
                    return true;
                }
                return false;
            });
        }
        // check if there is a customer in the session!
        static checkClient(nameSession) {
            return __awaiter(this, void 0, void 0, function* () {
                nameSession = this.replaceNameSession(nameSession);
                if (nameSession) {
                    // checks if the clinet name exists in the session
                    const checkFilter = this.session.filter((order) => order.name === nameSession);
                    // if it exists it returns a boolean true!
                    if (checkFilter.length) {
                        return true;
                    }
                }
                return false;
            });
        }
        // add new objects to the session!
        static addInfoSession(nameSession, exted) {
            return __awaiter(this, void 0, void 0, function* () {
                nameSession = this.replaceNameSession(nameSession);
                // checking if the session name and object have been passed!
                if (!!nameSession && typeof exted === 'object') {
                    // Using the function to check if the customer exists in the session!
                    const check = yield this.checkClient(nameSession);
                    if (check) {
                        // Scans all sessions to add a new object!
                        for (let i = 0; i < this.session.length; i += 1) {
                            if (this.session[i].name === nameSession) {
                                Object.assign(this.session[i], exted);
                                return true;
                            }
                        }
                    }
                }
                return false;
            });
        }
        // Get the client's index!
        static getSessionId(nameSession) {
            return __awaiter(this, void 0, void 0, function* () {
                nameSession = this.replaceNameSession(nameSession);
                if (nameSession) {
                    const check = yield this.checkClient(nameSession);
                    if (check) {
                        // Looks for the customer and returns the index!
                        for (let i = 0; i < this.session.length; i += 1) {
                            if (this.session[i].name === nameSession) {
                                return i;
                            }
                        }
                    }
                }
                return false;
            });
        }
        // Checks whether an index exists on the client object
        static checkObjectSession(nameSession, key, index) {
            return __awaiter(this, void 0, void 0, function* () {
                nameSession = this.replaceNameSession(nameSession);
                if (!!nameSession && !!key && Number.isInteger(index)) {
                    const check = yield this.checkClient(nameSession);
                    if (check) {
                        // Looking for the object, if it exists it returns a true!
                        if (this.session[index] && this.session[index][key]) {
                            return true;
                        }
                    }
                }
                return false;
            });
        }
        // remove an object added to the client
        static removeObjectsSession(nameSession, key, index) {
            return __awaiter(this, void 0, void 0, function* () {
                nameSession = this.replaceNameSession(nameSession);
                if (!!nameSession && !!key && !!index) {
                    const check = yield this.checkClient(nameSession);
                    if (check) {
                        if (this.session[index] && this.session[index][key]) {
                            delete this.session[index][key];
                            return true;
                        }
                    }
                }
                return false;
            });
        }
        // Removing a client from the session!
        static deleteSession(nameSession) {
            return __awaiter(this, void 0, void 0, function* () {
                nameSession = this.replaceNameSession(nameSession);
                if (nameSession) {
                    // Checking if the customer exists!
                    const check = yield this.checkClient(nameSession);
                    if (check) {
                        // If it exists, the client is deleted from the session!
                        let key = yield this.getSessionId(nameSession);
                        if (typeof key === 'number') {
                            this.session.splice(key, 1);
                            return true;
                        }
                    }
                }
                return false;
            });
        }
        // Returns all customer information!
        static getUser(nameSession) {
            return __awaiter(this, void 0, void 0, function* () {
                nameSession = this.replaceNameSession(nameSession);
                if (nameSession) {
                    const check = yield this.checkClient(nameSession);
                    if (check) {
                        let key = yield this.getSessionId(nameSession);
                        if (typeof key === 'number') {
                            return this.session[key];
                        }
                    }
                }
                return false;
            });
        }
        // Returns all sessions!
        static getAllSessions() {
            return __awaiter(this, void 0, void 0, function* () {
                return this.session;
            });
        }
    },
    _a.session = new Array(),
    _a);
//# sourceMappingURL=sessions.js.map