"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var bluebird_1 = __importDefault(require("bluebird"));
var Helpers_1 = __importDefault(require("./Helpers"));
var helpers = new Helpers_1.default();
var UserData = /** @class */ (function () {
    function UserData(api, storage, userId) {
        this.api = api;
        this.storage = storage;
        this.userId = userId;
        this.recentsLimit = 10;
        this._data = {};
        this._favs = [];
        this._links = [];
        this._workspaces = [];
        this._links = storage.user.get(this.META_RECENTS, []);
        return;
    }
    Object.defineProperty(UserData.prototype, "META_HIDE_TUTORIAL", {
        get: function () {
            return 'hide_tutorial';
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(UserData.prototype, "META_FAVS", {
        get: function () {
            return 'shortcuts';
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(UserData.prototype, "META_LOGO", {
        get: function () {
            return 'logo';
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(UserData.prototype, "META_RECENTS", {
        get: function () {
            return 'recents';
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(UserData.prototype, "META_WORKSPACES", {
        get: function () {
            return 'workspaces';
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(UserData.prototype, "META_ACTIVE_WORKSPACE", {
        get: function () {
            return 'active_workspace';
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(UserData.prototype, "favs", {
        get: function () {
            return this._favs;
        },
        enumerable: true,
        configurable: true
    });
    // convert key value from api data into object
    UserData.prototype.parse = function (data) {
        this._data = {};
        if (data === undefined) {
            return;
        }
        var i;
        for (i = 0; i < data.length; i++) {
            this._data[data[i].key] = data[i].value;
        }
        this._favs = this.get(this.META_FAVS, [], true) || [];
        // this._links = this.storage.user.get(this.META_RECENTS, []);
        this._workspaces = this.get(this.META_WORKSPACES, [], true) || [];
        return this._data;
    };
    UserData.prototype.set = function (key, value, stringify) {
        this._data[key] = value;
        return this.api.saveUserMetaData({
            userId: this.userId,
            data: [{ key: key, value: stringify ? JSON.stringify(value) : value }]
        });
    };
    UserData.prototype.refresh = function () {
        var _this = this;
        return this.api
            .getUserMetaData({
            userId: this.userId
        })
            .then(function (response) {
            return _this.parse(response.data);
        });
    };
    UserData.prototype.get = function (key, defaultValue, parse) {
        if (key) {
            if (this._data.hasOwnProperty(key)) {
                try {
                    return parse ? JSON.parse(this._data[key]) : this._data[key];
                }
                catch (e) {
                    return this._data[key];
                }
            }
            return defaultValue || null;
        }
        return this._data;
    };
    UserData.prototype.destroy = function () {
        this._data = {};
    };
    UserData.prototype.getRecents = function () {
        this._links = this.storage.user.get(this.META_RECENTS, []);
        return this._links;
    };
    UserData.prototype.updateRecent = function (data) {
        var _this = this;
        this._links.forEach(function (ln, index) {
            if (ln.link != data.link)
                return;
            _this._links[index] = data;
        });
        this.saveRecents();
    };
    UserData.prototype.addRecent = function (data) {
        var _this = this;
        this._links.forEach(function (ln, index) {
            if (ln.link == data.link) {
                _this._links.splice(index, 1);
            }
        });
        this._links.splice(0, 0, data);
        if (this._links.length > this.recentsLimit) {
            this._links.pop();
        }
        this.saveRecents();
    };
    UserData.prototype.getFavs = function () {
        var _this = this;
        var p = new bluebird_1.default(function (resolve, reject) {
            _this.refresh()
                .then(function () {
                resolve(_this._favs);
            })
                .catch(reject);
        });
        return p;
    };
    UserData.prototype.updateFav = function (data) {
        var _this = this;
        var p = new bluebird_1.default(function (resolve, reject) {
            _this.refresh()
                .then(function () {
                var index = -1;
                for (var i = 0; i < _this._favs.length; i++) {
                    if (_this._favs[i].link !== data.link)
                        continue;
                    index = i;
                    _this._favs[i] = data;
                }
                if (index < 0) {
                    resolve({ message: 'Favourite does not exist', favs: _this._favs });
                    return;
                }
                _this.set(_this.META_FAVS, _this._favs, true)
                    .then(function () {
                    resolve({ message: 'Favourite has been updated', favs: _this._favs });
                })
                    .catch(reject);
            })
                .catch(reject);
        });
        return p;
    };
    UserData.prototype.setFavName = function (folderId, pageId, title) {
        var link = this.getFavLink(folderId, pageId);
        var index = -1;
        for (var i = 0; i < this._favs.length; i++) {
            if (this._favs[i].link !== link)
                continue;
            index = i;
        }
        if (index < 0 || this._favs[index].title == title) {
            return false;
        }
        this._favs[index].title = title;
        return true;
    };
    UserData.prototype.saveFavs = function () {
        var _this = this;
        var p = new bluebird_1.default(function (resolve, reject) {
            _this.set(_this.META_FAVS, _this._favs, true)
                .then(function () {
                resolve({ message: 'Favourites has been updated', favs: _this._favs });
            })
                .catch(reject);
        });
        return p;
    };
    UserData.prototype.addFav = function (data) {
        var _this = this;
        var p = new bluebird_1.default(function (resolve, reject) {
            _this.refresh()
                .then(function () {
                if (!_this.checkForFavDuplicate(data.link)) {
                    _this._favs.push(data);
                    _this.set(_this.META_FAVS, _this._favs, true)
                        .then(function () {
                        resolve({ message: 'Favourite has been saved', favs: _this._favs });
                    })
                        .catch(reject);
                }
                else {
                    resolve({ message: 'Favourite already exists', favs: _this._favs });
                }
            })
                .catch(reject);
        });
        return p;
    };
    UserData.prototype.removeFav = function (data) {
        var _this = this;
        var p = new bluebird_1.default(function (resolve, reject) {
            _this.refresh()
                .then(function () {
                var index = -1;
                for (var i = 0; i < _this._favs.length; i++) {
                    if (_this._favs[i].link === data.link) {
                        _this._favs.splice(i, 1);
                        index = i;
                        break;
                    }
                }
                if (index < 0) {
                    resolve({ message: 'Favourite already removed', favs: _this._favs });
                    return;
                }
                _this.set(_this.META_FAVS, _this._favs, true)
                    .then(function () {
                    resolve({ message: 'Favourite has been removed', favs: _this._favs });
                })
                    .catch(reject);
            })
                .catch(reject);
        });
        return p;
    };
    UserData.prototype.toggleFav = function (data) {
        if (this.checkForFavDuplicate(data.link)) {
            return this.removeFav(data);
        }
        else {
            return this.addFav(data);
        }
    };
    UserData.prototype.getFavLink = function (folderId, pageId) {
        return "/domains/" + folderId + "/pages/" + pageId;
    };
    UserData.prototype.isFav = function (folderId, pageId) {
        return this.checkForFavDuplicate(this.getFavLink(folderId, pageId));
    };
    UserData.prototype.saveWorkspaces = function (workspaces) {
        var _this = this;
        var p = new bluebird_1.default(function (resolve, reject) {
            _this.refresh()
                .then(function () {
                _this.set(_this.META_WORKSPACES, workspaces, true)
                    .then(function () {
                    resolve({ message: 'Workspaces saved', workspaces: _this._workspaces });
                })
                    .catch(reject);
            })
                .catch(reject);
        });
        return p;
    };
    UserData.prototype.getNewWorkspace = function () {
        return {
            name: 'Workspace',
            boards: [],
            freeze: false,
            id: helpers.getUuid(),
            unit: '%'
        };
    };
    UserData.prototype.getWorkspaces = function () {
        var _this = this;
        var p = new bluebird_1.default(function (resolve, reject) {
            _this.refresh()
                .then(function () {
                resolve(_this._workspaces);
            })
                .catch(reject);
        });
        return p;
    };
    UserData.prototype.addWorkspace = function (data) {
        var _this = this;
        var p = new bluebird_1.default(function (resolve, reject) {
            _this.refresh()
                .then(function () {
                _this._workspaces.push(data);
                _this.set(_this.META_WORKSPACES, _this._workspaces, true)
                    .then(function () {
                    resolve({ message: 'Workspace has been added', workspaces: _this._workspaces });
                })
                    .catch(reject);
            })
                .catch(reject);
        });
        return p;
    };
    UserData.prototype.removeWorkspace = function (id) {
        var _this = this;
        var p = new bluebird_1.default(function (resolve, reject) {
            _this.refresh()
                .then(function () {
                var index = -1;
                for (var i = 0; i < _this._workspaces.length; i++) {
                    if (_this._workspaces[i].id === id) {
                        _this._workspaces.splice(i, 1);
                        index = i;
                        break;
                    }
                }
                if (index < 0) {
                    resolve({ message: 'Workspace already removed', workspaces: _this._workspaces });
                    return;
                }
                _this.set(_this.META_WORKSPACES, _this._workspaces, true)
                    .then(function () {
                    resolve({ message: 'Workspace has been removed', workspaces: _this._workspaces });
                })
                    .catch(reject);
            })
                .catch(reject);
        });
        return p;
    };
    UserData.prototype.setWorkspaces = function (data) {
        return this.set(this.META_WORKSPACES, JSON.stringify(data));
    };
    UserData.prototype.checkForFavDuplicate = function (link) {
        var index = -1;
        for (var i = 0; i < this._favs.length; i++) {
            if (this._favs[i].link === link) {
                index = 1;
                break;
            }
        }
        return index >= 0;
    };
    UserData.prototype.saveRecents = function () {
        this.storage.user.save(this.META_RECENTS, JSON.stringify(this._links));
    };
    return UserData;
}());
exports.default = UserData;
//# sourceMappingURL=UserData.js.map