/**
 * @since Last modified: 2024/05/11 17:11:46
 * @namespace instance
 * @description Instance management. All instances can be found in a global scope and can be destroyed or initialized in batches.
 * @see {@link https://codepen.io/axui/pen/ExrvEmy|demo @ codepen}
 */
'use strict';
const instance = {
    /**
 * @memberOf instance
 * @name data
 * @description All instance collection.Format：[{type:'dialog',name:'xx',ins:'',pushTime:''},...].
 * @type {array}
 */
    data: [],
    destroyFun: (item) => {
        if (!item) {
            return false;
        }
        if ((!item.ins.hasOwnProperty('destroyed') || !item.ins.destroyed) && item.ins.__proto__.destroy) {
            item.ins.destroy();
            item.destTime = new Date().getTime();
        }
    },
    initFun: (item) => {
        if (!item) {
            return false;
        }
        if ((!item.ins.hasOwnProperty('destroyed') || item.ins.destroyed) && item.ins.__proto__.init) {
            item.ins.init();
            item.initTime = new Date().getTime();
        }
    },
    /**
 * @memberOf instance
 * @description Add the instance to the collection array.
 * @param {*} ins - A instance.
 * @param {string} [name=''] - Instance name.
 * @param {string} [type=''] - Instance type.
 * @returns {instance|false} - Return instance or false.
 * @see {@link https://codepen.io/axui/pen/ExrvEmy|demo @ codepen}
 * @example
 * ax.instance.push(ins);
 */
    push: function (ins, name = '', type = '') {
        if (!ins) {
            return false;
        }
        let obj = { name, ins, type, pushTime: new Date().getTime() };
        if (!this.data.some((k) => k.ins === ins)) {
            this.data.push(obj);
        }
        return this;
    },
    /**
* @memberOf instance
* @description Find the instance from the collection array.
* @param {string} name - Instance name.
* @param {string} [type=''] - Instance type.
* @param {boolean} [destroyed=false] - Destroy state of instance.
* @returns {*|null} - Return a instance.
 * @see {@link https://codepen.io/axui/pen/ExrvEmy|demo @ codepen}
* @example
* ax.instance.find('demo');
*/
    find: function (name, type = '', destroyed = false) {
        if (!name) {
            return null;
        }
        let item;
        item = this.data.find((k) => {
            let flag = type ? k.type === type : true;
            return (k.ins.hasOwnProperty('destroyed')) ? (k.name === name && k.ins.destroyed === destroyed && flag) : (k.name === name && flag);
        });
        return item ? item.ins : null;
    },
    /**
* @memberOf instance
* @description Find instances of a type from the collection array.
* @param {string} [type=''] - Instance type.
* @param {boolean} [destroyed=false] - Destroy state of instance.
* @returns {array} - Return the instances array.
* @see {@link https://codepen.io/axui/pen/ExrvEmy|demo @ codepen}
* @example
* ax.instance.findAll('dialog');
*/
    findAll: function (type = '', destroyed = false) {
        let items = [];
        if (!type) {
            items = this.data.filter((i) => i.ins.destroyed === destroyed);
        }
        else {
            items = this.data.filter((i) => {
                return (i.ins.hasOwnProperty('destroyed')) ? (i.type === type && i.ins.destroyed === destroyed) : (i.type === type);
            });
        }
        return items.length > 0 ? items.map((i) => i.ins) : [];
    },
    /**
* @memberOf instance
* @description Destroy a instance from the collection array.
* @param {string} name - Instance name.
* @param {string} type - Instance type.
* @returns {instance|false} Return instance or false.
* @see {@link https://codepen.io/axui/pen/ExrvEmy|demo @ codepen}
* @example
* ax.instance.destroy('demo','dialog');
*/
    destroy: function (name, type) {
        if (!name) {
            return false;
        }
        let item = type ? (this.data.find((i) => i.name === name && i.type === type)) : (this.data.find((i) => i.name === name));
        if (item) {
            this.destroyFun(item);
        }
        return this;
    },
    /**
    * @memberOf instance
    * @description Destroy instances of a type from the collection array.
    * @param {string} [type] - Instance type.
    * @returns {instance} Return instance.
    * @see {@link https://codepen.io/axui/pen/ExrvEmy|demo @ codepen}
    * @example
    * ax.instance.destroyAll('dialog');
    */
    destroyAll: function (type) {
        let items = !type ? this.data : this.data.filter((i) => i.type === type);
        items.forEach((i) => {
            this.destroyFun(i);
        });
        return this;
    },
    /**
    * @memberOf instance
    * @description Destroy all instances and empty the collection array.
    * @returns {instance} Return instance.
    * @see {@link https://codepen.io/axui/pen/ExrvEmy|demo @ codepen}
    * @example
    * ax.instance.destroyAll('dialog');
    */
    clear: function () {
        this.data.forEach((i) => {
            this.destroyFun(i);
        });
        this.data.length = 0;
        return this;
    },
    /**
    * @memberOf instance
    * @description Init a instance.
    * @param {string} name - Instance name.
    * @param {string} [type] - Instance type.
    * @returns {instance|false} Return instance or false.
    * @see {@link https://codepen.io/axui/pen/ExrvEmy|demo @ codepen}
    * @example
    * ax.instance.init('demo'[,'dialog']);
    */
    init: function (name, type) {
        if (!name) {
            return false;
        }
        let item = type ? (this.data.find((i) => i.name === name && i.type === type)) : (this.data.find((i) => i.name === name));
        if (item) {
            this.initFun(item);
        }
        return this;
    },
    /**
    * @memberOf instance
    * @description Init instances of a type.
    * @param {string} [type] - Instance type.
    * @returns {instance} Return instance.
    * @see {@link https://codepen.io/axui/pen/ExrvEmy|demo @ codepen}
    * @example
    * ax.instance.initAll('dialog');
    */
    initAll: function (type) {
        let items = !type ? this.data : this.data.filter((i) => i.type === type);
        items.forEach((i) => {
            this.initFun(i);
        });
        return this;
    }
};
export default instance;
