var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
  if (kind === "a" && !f)
    throw new TypeError("Private accessor was defined without a getter");
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
    throw new TypeError("Cannot read private member from an object whose class did not declare it");
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
  if (kind === "m")
    throw new TypeError("Private method is not writable");
  if (kind === "a" && !f)
    throw new TypeError("Private accessor was defined without a setter");
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver))
    throw new TypeError("Cannot write private member to an object whose class did not declare it");
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var _KlevuChatLayout_instances, _KlevuChatLayout_scrollElement, _KlevuChatLayout_popupElement, _KlevuChatLayout_sendMessage;
import { Host, h } from "@stencil/core";
import { globalExportedParts } from "../../utils/utils";
/**
 * Component that wraps chat elements into a layout.
 *
 * @cssprop --klevu-chat-layout-max-height 100vh The maxium height for the chat layout.
 */
export class KlevuChatLayout {
  constructor() {
    _KlevuChatLayout_instances.add(this);
    _KlevuChatLayout_scrollElement.set(this, void 0);
    _KlevuChatLayout_popupElement.set(this, void 0);
    this.showLoading = false;
    this.showClose = false;
    this.text = "";
  }
  componentDidLoad() {
    var _a;
    // listen to updates to DOM in slot and scroll to bottom
    const main = (_a = this.el.shadowRoot) === null || _a === void 0 ? void 0 : _a.querySelector("slot");
    const observer = new MutationObserver(() => {
      this.scrollMainToBottom();
    });
    if (main) {
      observer.observe(main, { childList: true });
    }
    setTimeout(() => {
      this.scrollMainToBottom();
    }, 20);
  }
  /**
   * Scroll current chat to bottom of page
   */
  async scrollMainToBottom() {
    var _a;
    const instance = await ((_a = __classPrivateFieldGet(this, _KlevuChatLayout_scrollElement, "f")) === null || _a === void 0 ? void 0 : _a.getInstance());
    if (instance) {
      setTimeout(() => {
        instance.elements().viewport.scrollTo({
          top: instance.elements().viewport.scrollHeight,
          behavior: "smooth",
        });
      }, 20);
    }
  }
  /**
   * Close the popup menu
   */
  async closePopup() {
    var _a;
    (_a = __classPrivateFieldGet(this, _KlevuChatLayout_popupElement, "f")) === null || _a === void 0 ? void 0 : _a.closeModal();
  }
  render() {
    return (h(Host, null, h("header", null, h("klevu-typography", { variant: "body-m-bold" }, "MOI"), this.showClose && (h("klevu-button", { onClick: () => this.klevuChatLayoutClose.emit(), size: "small", exportparts: globalExportedParts, icon: "close", isSecondary: true }))), h("main", null, h("klevu-util-scrollbars", { overflowX: "hidden", overflowY: "scroll", ref: (el) => (__classPrivateFieldSet(this, _KlevuChatLayout_scrollElement, el, "f")) }, h("slot", null))), h("footer", null, h("slot", { name: "actions" }), h("div", { class: "inputs" }, h("klevu-popup", { anchor: "top-start", ref: (el) => (__classPrivateFieldSet(this, _KlevuChatLayout_popupElement, el, "f")) }, h("div", { slot: "content" }, h("slot", { name: "menu" })), h("klevu-button", { slot: "origin", exportparts: globalExportedParts, icon: "menu", isSecondary: true })), h("klevu-textfield", { variant: "pill", value: this.text, onKlevuTextChanged: (e) => {
        this.text = e.detail;
        e.preventDefault();
        return false;
      }, onKlevuTextEnterPressed: () => __classPrivateFieldGet(this, _KlevuChatLayout_instances, "m", _KlevuChatLayout_sendMessage).call(this) }), h("klevu-button", { exportparts: globalExportedParts, icon: "send", onClick: () => __classPrivateFieldGet(this, _KlevuChatLayout_instances, "m", _KlevuChatLayout_sendMessage).call(this) })))));
  }
  static get is() { return "klevu-chat-layout"; }
  static get encapsulation() { return "shadow"; }
  static get originalStyleUrls() {
    return {
      "$": ["klevu-chat-layout.css"]
    };
  }
  static get styleUrls() {
    return {
      "$": ["klevu-chat-layout.css"]
    };
  }
  static get properties() {
    return {
      "showLoading": {
        "type": "boolean",
        "mutable": false,
        "complexType": {
          "original": "boolean",
          "resolved": "boolean",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": "Show loading indicator"
        },
        "attribute": "show-loading",
        "reflect": false,
        "defaultValue": "false"
      },
      "showClose": {
        "type": "boolean",
        "mutable": false,
        "complexType": {
          "original": "boolean",
          "resolved": "boolean",
          "references": {}
        },
        "required": false,
        "optional": false,
        "docs": {
          "tags": [],
          "text": ""
        },
        "attribute": "show-close",
        "reflect": false,
        "defaultValue": "false"
      }
    };
  }
  static get states() {
    return {
      "text": {}
    };
  }
  static get events() {
    return [{
        "method": "klevuChatLayoutMessageSent",
        "name": "klevuChatLayoutMessageSent",
        "bubbles": true,
        "cancelable": true,
        "composed": true,
        "docs": {
          "tags": [],
          "text": "Event emitted when user sends a message"
        },
        "complexType": {
          "original": "string",
          "resolved": "string",
          "references": {}
        }
      }, {
        "method": "klevuChatLayoutClose",
        "name": "klevuChatLayoutClose",
        "bubbles": true,
        "cancelable": true,
        "composed": true,
        "docs": {
          "tags": [],
          "text": "Event emitted when user closes the chat layout"
        },
        "complexType": {
          "original": "void",
          "resolved": "void",
          "references": {}
        }
      }];
  }
  static get methods() {
    return {
      "scrollMainToBottom": {
        "complexType": {
          "signature": "() => Promise<void>",
          "parameters": [],
          "references": {
            "Promise": {
              "location": "global"
            }
          },
          "return": "Promise<void>"
        },
        "docs": {
          "text": "Scroll current chat to bottom of page",
          "tags": []
        }
      },
      "closePopup": {
        "complexType": {
          "signature": "() => Promise<void>",
          "parameters": [],
          "references": {
            "Promise": {
              "location": "global"
            }
          },
          "return": "Promise<void>"
        },
        "docs": {
          "text": "Close the popup menu",
          "tags": []
        }
      }
    };
  }
  static get elementRef() { return "el"; }
}
_KlevuChatLayout_scrollElement = new WeakMap(), _KlevuChatLayout_popupElement = new WeakMap(), _KlevuChatLayout_instances = new WeakSet(), _KlevuChatLayout_sendMessage = function _KlevuChatLayout_sendMessage() {
  this.klevuChatLayoutMessageSent.emit(this.text);
  this.text = "";
};
//# sourceMappingURL=klevu-chat-layout.js.map
