"use strict";
var __rest = (this && this.__rest) || function (s, e) {
    var t = {};
    for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
        t[p] = s[p];
    if (s != null && typeof Object.getOwnPropertySymbols === "function")
        for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
            if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
                t[p[i]] = s[p[i]];
        }
    return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Room = void 0;
const Gateway_1 = require("../remote-services/Gateway");
const roomCache = new Map();
class Building {
    constructor(data) {
        if (data)
            this.setData(data);
    }
    setData(data) {
        if (data.id)
            this.id = parseInt(data.id);
        if (data.name)
            this.name = data.name;
        if (data.code)
            this.code = data.code;
        if (data.path)
            this.path = data.path;
    }
    get fullName() {
        const units = [];
        if (this.path) {
            units.push(...this.path.split('/'));
        }
        units.push(this.name);
        return units.join('');
    }
    toJSON() {
        return {
            id: this.id,
            name: this.name,
            code: this.code,
            path: this.path,
        };
    }
}
class Room {
    constructor(data) {
        if (data)
            this.setData(data);
    }
    setData(data) {
        const { building, building_id, group_id } = data, attrs = __rest(data, ["building", "building_id", "group_id"]);
        if (group_id)
            this.groupId = group_id;
        if (building)
            this.building = new Building(building);
        if (building_id)
            this.buildingId = building_id;
        Object.keys(attrs).forEach((key) => {
            this[key] = attrs[key];
        });
    }
    toJSON() {
        const json = {
            id: this.id,
            name: this.name,
            code: this.code,
            groupId: this.groupId,
        };
        if (this.building) {
            json.building = this.building.toJSON();
        }
        return json;
    }
    get codeOrName() {
        if (this.code && this.name && this.name !== this.code) {
            return `${this.code} ${this.name}`;
        }
        return this.code || this.name;
    }
    get fullName() {
        var _a;
        const units = [];
        if ((_a = this.building) === null || _a === void 0 ? void 0 : _a.id) {
            if (this.building.path) {
                units.push(...this.building.path.split('/'));
            }
            units.push(this.building.name);
        }
        units.push(this.codeOrName);
        for (let i = 0; i < units.length - 1; i++) {
            if (units[i].charCodeAt(units[i].length - 1) > 128 || units[i + 1].charCodeAt(0) > 128) {
                continue;
            }
            else {
                units[i] += ' ';
            }
        }
        return units.join('');
    }
    relativeName(refRoom) {
        var _a, _b;
        if (!refRoom)
            return this.fullName;
        let name = this.name;
        if (((_a = refRoom.building) === null || _a === void 0 ? void 0 : _a.id) === ((_b = this.building) === null || _b === void 0 ? void 0 : _b.id)) {
            return name;
        }
        return this.fullName;
    }
    static async findByPk(id, token) {
        if (!token)
            return;
        let room = roomCache.get(id);
        if (!room) {
            room = new Promise(async (resolve, reject) => {
                try {
                    const gateway = new Gateway_1.default(token);
                    const data = await gateway.get({
                        path: `v1/room/${id}`,
                        query: { includes: 'building' },
                    });
                    resolve(new Room(data));
                }
                catch (e) {
                    reject(e);
                }
            });
            roomCache.set(id, room);
        }
        return room;
    }
}
exports.Room = Room;
