import Modal from "./modal";
import StringUtil from "../../util/StringUtil";
class ModalManager{
    constructor(){
    }
    
    static open(data){
        if(data){
            let modal = new Modal(data);
            ModalManager.modalPool.push(modal);
            return modal;
        }
        return null;
    }
    
    static close(idOrModal=null){
        let result = false;
        if(ModalManager.modalPool.length>0){
            let modal = null;
            if(idOrModal==null) {
                modal = ModalManager.modalPool.pop();
            }
            else{
                if(StringUtil.isObject(idOrModal) && idOrModal.__name == "Modal"){
                    modal = idOrModal;
                }
                else{
                    modal = ModalManager.findModal(idOrModal);
                }
            }
            if(modal){
                modal.close();
                result = true;
            }
        }
        return result;
    }
    static findModal(id){
        let result = null;
        let list = ModalManager.modalPool;
        if(list.length>0){
            let len = list.length;
            for(let i=0;i<len;i++){
                let m = list[i];
                if(m.id == id){
                    result = m;
                    break;
                }
            }
        }
        return result;
    }
}
ModalManager.modalPool = [];
export default ModalManager;