import { Host, h } from '@stencil/core';
import Toastify from 'toastify-js';
import "toastify-js/src/toastify.css";
export class BsToast {
  constructor() {
    this.name = undefined;
    this.message = undefined;
    this.position = "right";
    this.gravity = "top";
    this.show = false;
    this.autohide = true;
    this.delay = 3000;
    this.href = undefined;
    this.newWindow = true;
    this.canClose = false;
    this.hasOpened = false;
    this.delayRemove = undefined;
  }
  /**
   * Opens the toast
   */
  async toastOpen() {
    clearTimeout(this.delayRemove);
    if (this.hasOpened) {
      this.toast.hideToast();
      this.delayRemove = setTimeout(() => {
        this.toast.showToast();
      }, 500);
    }
    else {
      this.toast.showToast();
    }
    this.open.emit();
    this.hasOpened = true;
  }
  /**
   * Closes the toast
   */
  async toastClose() {
    this.toast.hideToast();
    this.hasOpened = false;
    this.close.emit();
  }
  componentDidLoad() {
    if (this.message && !this.name) {
      this.toast = Toastify({
        text: this.message,
        duration: this.autohide ? this.delay : -1,
        newWindow: this.newWindow,
        destination: this.href,
        close: this.canClose,
        gravity: this.gravity,
        position: this.position,
        backgroundColor: "rgba(255, 255, 255, 0.85)",
        className: "custom-toastify",
        stopOnFocus: true,
      });
    }
    else {
      this.toast = Toastify({
        text: this.name,
        node: this.el.shadowRoot.querySelector(".toast").cloneNode(true),
        duration: this.autohide ? this.delay : -1,
        newWindow: true,
        close: this.canClose,
        gravity: this.gravity,
        position: this.position,
        backgroundColor: "transparent",
        stopOnFocus: true,
      });
    }
    if (this.show === true) {
      this.toast.showToast();
    }
  }
  render() {
    return (h(Host, null, h("div", { class: "toast fade show", role: "alert", "aria-live": "assertive", "aria-atomic": "true" }, h("div", { class: "toast-header" }, h("strong", { class: "mr-auto" }, this.name)), h("div", { class: "toast-body" }, this.message))));
  }
  static get is() { return "bs-toast"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() {
    return {
      "$": ["bs-toast.scss"]
    };
  }
  static get styleUrls() {
    return {
      "$": ["bs-toast.css"]
    };
  }
  static get properties() {
    return {
      "name": {
        "type": "string",
        "mutable": false,
        "complexType": {
          "original": "string",
          "resolved": "string",
          "references": {}
        },
        "required": false,
        "optional": true,
        "docs": {
          "tags": [],
          "text": "The header name of the toast"
        },
        "attribute": "name",
        "reflect": false
      },
      "message": {
        "type": "string",
        "mutable": false,
        "complexType": {
          "original": "string",
          "resolved": "string",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "The header name of the toast"
        },
        "attribute": "message",
        "reflect": false
      },
      "position": {
        "type": "string",
        "mutable": false,
        "complexType": {
          "original": "\"left\" | \"center\" | \"right\"",
          "resolved": "\"center\" | \"left\" | \"right\"",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Position of the toast"
        },
        "attribute": "position",
        "reflect": false,
        "defaultValue": "\"right\""
      },
      "gravity": {
        "type": "string",
        "mutable": false,
        "complexType": {
          "original": "\"top\" | \"bottom\"",
          "resolved": "\"bottom\" | \"top\"",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Position of the toast"
        },
        "attribute": "gravity",
        "reflect": false,
        "defaultValue": "\"top\""
      },
      "show": {
        "type": "boolean",
        "mutable": false,
        "complexType": {
          "original": "boolean",
          "resolved": "boolean",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "When `true` The toast will show itself."
        },
        "attribute": "show",
        "reflect": false,
        "defaultValue": "false"
      },
      "autohide": {
        "type": "boolean",
        "mutable": false,
        "complexType": {
          "original": "boolean",
          "resolved": "boolean",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Delay hiding the toast (ms)"
        },
        "attribute": "autohide",
        "reflect": false,
        "defaultValue": "true"
      },
      "delay": {
        "type": "number",
        "mutable": false,
        "complexType": {
          "original": "number",
          "resolved": "number",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "\nDelay hiding the toast (ms)"
        },
        "attribute": "delay",
        "reflect": false,
        "defaultValue": "3000"
      },
      "href": {
        "type": "string",
        "mutable": false,
        "complexType": {
          "original": "string",
          "resolved": "string",
          "references": {}
        },
        "required": false,
        "optional": true,
        "docs": {
          "tags": [],
          "text": "toast destination"
        },
        "attribute": "href",
        "reflect": false
      },
      "newWindow": {
        "type": "boolean",
        "mutable": false,
        "complexType": {
          "original": "boolean",
          "resolved": "boolean",
          "references": {}
        },
        "required": false,
        "optional": true,
        "docs": {
          "tags": [],
          "text": "href in newWindow"
        },
        "attribute": "new-window",
        "reflect": false,
        "defaultValue": "true"
      },
      "canClose": {
        "type": "boolean",
        "mutable": false,
        "complexType": {
          "original": "boolean",
          "resolved": "boolean",
          "references": {}
        },
        "required": false,
        "optional": true,
        "docs": {
          "tags": [],
          "text": "make toast closable"
        },
        "attribute": "can-close",
        "reflect": false,
        "defaultValue": "false"
      }
    };
  }
  static get states() {
    return {
      "hasOpened": {},
      "delayRemove": {}
    };
  }
  static get events() {
    return [{
        "method": "close",
        "name": "close",
        "bubbles": true,
        "cancelable": true,
        "composed": true,
        "docs": {
          "tags": [],
          "text": "Emitted on close of toast"
        },
        "complexType": {
          "original": "any",
          "resolved": "any",
          "references": {}
        }
      }, {
        "method": "open",
        "name": "open",
        "bubbles": true,
        "cancelable": true,
        "composed": true,
        "docs": {
          "tags": [],
          "text": "Emitted on the open of toast"
        },
        "complexType": {
          "original": "any",
          "resolved": "any",
          "references": {}
        }
      }];
  }
  static get methods() {
    return {
      "toastOpen": {
        "complexType": {
          "signature": "() => Promise<void>",
          "parameters": [],
          "references": {
            "Promise": {
              "location": "global"
            }
          },
          "return": "Promise<void>"
        },
        "docs": {
          "text": "Opens the toast",
          "tags": []
        }
      },
      "toastClose": {
        "complexType": {
          "signature": "() => Promise<void>",
          "parameters": [],
          "references": {
            "Promise": {
              "location": "global"
            }
          },
          "return": "Promise<void>"
        },
        "docs": {
          "text": "Closes the toast",
          "tags": []
        }
      }
    };
  }
  static get elementRef() { return "el"; }
}
//# sourceMappingURL=bs-toast.js.map
