import { SnackBarOptions } from './SnackbarOptions';
export class SnackbarController {
    constructor() {
        this.snackQueue = [];
    }
    async create(options) {
        this.controllerEl = document.querySelector('materials-snackbar-controller');
        if (!this.controllerEl) {
            this.controllerEl = document.body.appendChild(document.createElement('materials-snackbar-controller'));
        }
        const snack = document.createElement('materials-snackbar');
        Object.assign(snack, new SnackBarOptions(), options);
        snack.addEventListener('close', (ev) => {
            const closedSnack = ev.target;
            closedSnack.remove();
            this.controllerEl.sliceQueue();
        });
        this.controllerEl.pushToQueue(snack);
    }
    async pushToQueue(snack) {
        this.snackQueue = [...this.snackQueue, snack];
    }
    async sliceQueue() {
        this.snackQueue = this.snackQueue.length > 1 ? [...this.snackQueue.slice(1, this.snackQueue.length)] : [];
    }
    handleQueue(newValue, oldValue) {
        if (newValue.length > 0 && (oldValue.length > newValue.length || newValue.length === 1)) {
            document.body.appendChild(newValue[0]);
        }
    }
    static get is() { return "materials-snackbar-controller"; }
    static get encapsulation() { return "shadow"; }
    static get states() { return {
        "snackQueue": {}
    }; }
    static get methods() { return {
        "create": {
            "complexType": {
                "signature": "(options: SnackBarOptions) => Promise<void>",
                "parameters": [{
                        "tags": [],
                        "text": ""
                    }],
                "references": {
                    "Promise": {
                        "location": "global"
                    },
                    "SnackBarOptions": {
                        "location": "import",
                        "path": "./SnackbarOptions"
                    },
                    "HTMLMaterialsSnackbarElement": {
                        "location": "global"
                    }
                },
                "return": "Promise<void>"
            },
            "docs": {
                "text": "",
                "tags": []
            }
        },
        "pushToQueue": {
            "complexType": {
                "signature": "(snack: HTMLMaterialsSnackbarElement) => Promise<void>",
                "parameters": [{
                        "tags": [],
                        "text": ""
                    }],
                "references": {
                    "Promise": {
                        "location": "global"
                    },
                    "HTMLMaterialsSnackbarElement": {
                        "location": "global"
                    }
                },
                "return": "Promise<void>"
            },
            "docs": {
                "text": "",
                "tags": []
            }
        },
        "sliceQueue": {
            "complexType": {
                "signature": "() => Promise<void>",
                "parameters": [],
                "references": {
                    "Promise": {
                        "location": "global"
                    }
                },
                "return": "Promise<void>"
            },
            "docs": {
                "text": "",
                "tags": []
            }
        }
    }; }
    static get elementRef() { return "host"; }
    static get watchers() { return [{
            "propName": "snackQueue",
            "methodName": "handleQueue"
        }]; }
}
