/**
 * @since Last modified: 2023-11-15 12:40:43
 * @namespace localStorage
 * @description Caches the data locally.
 * @property {function} set - Store or modify cache data.
 * @property {function} get - Get the cache data.
 * @property {function} remove - Remove the cache data.
 * @property {function} clear - Clear cache data.
 * @see {@link https://codepen.io/axui/pen/abXwOXm|demo @ codepen}
 * @example
 * ax.localStorage.set('myData',{a:0,b:1});
 */
'use strict';
import deepClone from './deepClone';
import getDataType from './getDataType';
import isEmpty from './isEmpty';
import extend from './extend';
import getExpireDate from './getExpireDate';
const localStorage = {
    /**
 * @memberOf localStorage
 * @description Storage cache data.
 * @param {string} key - The name of cache data.
 * @param {*} value - The value of cache data.
 * @param {object} [options] - The options of cache data.
 * @param {string} [options.type=d] - The date unit that calculates the expiration time.The default value is 'd'(day).
 * @param {number|string} [options.expires=0] - The value of The date unit.The default value is 365.The default value is 0(Never expire).
 * @param {boolean} [options.cover=true] - Whether the original value is covered in the same case.The default value is true.
 * @returns {*} Return localStorage itself.
 * @see {@link https://codepen.io/axui/pen/abXwOXm|demo @ codepen}
 * @example
 * ax.localStorage.set('myData',{a:0,b:1});
 */
    set: function (key, value, options = { type: 'd', expires: 0, cover: true }) {
        if (isEmpty(key) && typeof key !== 'string') {
            return false;
        }
        let newValue = deepClone(value), type = getDataType(value), filterProps = (obj) => {
            Object.keys(obj).forEach(k => {
                let val = obj[k];
                (getDataType(val) === 'Array' || getDataType(val) === 'Object') && filterProps(val);
                (getDataType(val).includes('HTML') || getDataType(val) === 'Function') && delete obj[k];
            });
            return obj;
        };
        if ((type === 'Array' || type === 'Object') && !isEmpty(newValue)) {
            filterProps(newValue);
        }
        let expires = options.expires !== 0 ? getExpireDate(options.type, options.expires) : 0, oldValue = localStorage.get(key), valueExp = {
            data: (!options.cover && localStorage.get(key)) ? extend(oldValue, newValue) : newValue,
            expires: expires,
        };
        if (((typeof options.expires === 'number' && options.expires < 0) || newValue === null) && this.get(key)) {
            this.del(key);
        }
        else {
            window.localStorage.setItem(key, JSON.stringify(valueExp));
        }
        return this;
    },
    /**
* @memberOf localStorage
* @description Get cache data.
* @param {string} key - The name of cache data.
* @returns {string|null} Return localStorage data.
* @see {@link https://codepen.io/axui/pen/abXwOXm|demo @ codepen}
* @example
* ax.localStorage.get('myData');
*/
    get: function (key) {
        if (isEmpty(key) || typeof key !== 'string') {
            return false;
        }
        let now = new Date().toUTCString(), valueExp = JSON.parse(window.localStorage.getItem(key) || '');
        if (valueExp) {
            if (valueExp.expires === 0 || now <= valueExp.expires) {
                return valueExp.data;
            }
            else {
                window.localStorage.removeItem(key);
                return null;
            }
        }
        else {
            return null;
        }
    },
    /**
* @memberOf localStorage
* @description Remove cache data.
* @param {string} key - The name of cache data.
* @returns {string|null} Return localStorage itself.
* @see {@link https://codepen.io/axui/pen/abXwOXm|demo @ codepen}
* @example
* ax.localStorage.remove('myData');
*/
    remove: function (key) {
        if (isEmpty(key) || typeof key !== 'string') {
            return false;
        }
        window.localStorage.removeItem(key);
        return this;
    },
    /**
* @memberOf localStorage
* @description Clear all cache data.
* @returns {string|null} Return localStorage itself.
* @see {@link https://codepen.io/axui/pen/abXwOXm|demo @ codepen}
* @example
* ax.localStorage.clear();
*/
    clear: function () {
        window.localStorage.clear();
        return this;
    }
};
export default localStorage;
