"use strict";
/*
 * @adonisjs/redis
 *
 * (c) Harminder Virk <virk@adonisjs.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.Store = void 0;
/// <reference path="../../adonis-typings/index.ts" />
const utils_1 = require("@poppinss/utils");
/**
 * Session store to mutate and access values from the session object
 */
class Store {
    constructor(values) {
        this.values = values || {};
    }
    /**
     * Find if store is empty or not
     */
    get isEmpty() {
        return !this.values || Object.keys(this.values).length === 0;
    }
    /**
     * Set key/value pair
     */
    set(key, value) {
        utils_1.lodash.set(this.values, key, value);
    }
    /**
     * Get value for a given key
     */
    get(key, defaultValue) {
        return utils_1.lodash.get(this.values, key, defaultValue);
    }
    /**
     * Remove key
     */
    unset(key) {
        utils_1.lodash.unset(this.values, key);
    }
    /**
     * Reset store by clearing it's values.
     */
    clear() {
        this.update({});
    }
    /**
     * Pull value from the store. It is same as calling
     * store.get and then store.unset
     */
    pull(key, defaultValue) {
        return ((value) => {
            this.unset(key);
            return value;
        })(this.get(key, defaultValue));
    }
    /**
     * Increment number. The method raises an error when
     * nderlying value is not a number
     */
    increment(key, steps = 1) {
        const value = this.get(key, 0);
        if (typeof value !== 'number') {
            throw new utils_1.Exception(`Cannot increment "${key}", since original value is not a number`);
        }
        this.set(key, value + steps);
    }
    /**
     * Increment number. The method raises an error when
     * nderlying value is not a number
     */
    decrement(key, steps = 1) {
        const value = this.get(key, 0);
        if (typeof value !== 'number') {
            throw new utils_1.Exception(`Cannot increment "${key}", since original value is not a number`);
        }
        this.set(key, value - steps);
    }
    /**
     * Overwrite the underlying values object
     */
    update(values) {
        this.values = values;
    }
    /**
     * Update to merge values
     */
    merge(values) {
        utils_1.lodash.merge(this.values, values);
    }
    /**
     * A boolean to know if value exists. Extra guards to check
     * arrays for it's length as well.
     */
    has(key, checkForArraysLength = true) {
        const value = this.get(key);
        if (!Array.isArray(value)) {
            return !!value;
        }
        return checkForArraysLength ? value.length > 0 : !!value;
    }
    /**
     * Get all values
     */
    all() {
        return this.values;
    }
    /**
     * Returns object representation of values
     */
    toObject() {
        return this.all();
    }
    /**
     * Returns the store values
     */
    toJSON() {
        return this.all();
    }
    /**
     * Returns string representation of the store
     */
    toString() {
        return JSON.stringify(this.all());
    }
}
exports.Store = Store;
