import { unsafeCSS, LitElement, nothing } from "lit";
import { property, query, state } from "lit/decorators.js";
import compentStyle from "./card.css.js";
import { literal, html } from "lit-html/static.js";
import "../icon-button/icon-button.js";
import "../../icons/icon-close-google.js";
import "../../icons/icon-chevron-right-google.js";
import { classMap } from "lit/directives/class-map.js";
import { customElement } from "../../decorator.js";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __decorateClass = (decorators, target, key, kind) => {
  var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
  for (var i = decorators.length - 1, decorator; i >= 0; i--)
    if (decorator = decorators[i])
      result = (kind ? decorator(target, key, result) : decorator(result)) || result;
  if (kind && result) __defProp(target, key, result);
  return result;
};
let ObcCard = class extends LitElement {
  constructor() {
    super(...arguments);
    this.showTitle = true;
    this.hasDialog = false;
    this.dialogTimeOutSeconds = 2e4;
    this.dialogVisibleTimerSeconds = 1e4;
    this.countdownSeconds = 0;
    this.showCountdown = false;
  }
  render() {
    const wrapperTag = this.hasDialog ? literal`button` : literal`section`;
    return html`
      <${wrapperTag} class=${classMap({ wrapper: true, "has-dialog": this.hasDialog })} @click=${this.openDialog}>
        ${!this.showTitle ? nothing : html`<div class="header">
                <div></div>
                <div class="title">
                  <slot name="title"></slot>
                </div>
                ${this.hasDialog ? html`
                      <obi-chevron-right-google
                        class="icon"
                      ></obi-chevron-right-google>
                    ` : html`<div></div>`}
              </div>`}
        <div class="content">
          <slot></slot>
        </div>
      </${wrapperTag}>
      ${this.hasDialog ? html`
              <dialog class="dialog-wrapper" closedby="any" popover>
                <div class="header">
                  <div></div>
                  <div class="title">
                    <slot name="dialog-title"></slot>
                  </div>
                  <div class="actions">
                    <obc-icon-button
                      @click=${this.closeDialog}
                      variant="flat"
                      .progress=${this.showCountdown ? this.getProgressPercentage() : void 0}
                    >
                      <obi-close-google></obi-close-google>
                    </obc-icon-button>
                  </div>
                </div>

                <div class="content">
                  <slot name="dialog-content"></slot>
                </div>
              </dialog>
            ` : ""}
    `;
  }
  closeDialog(e) {
    e.stopPropagation();
    this.clearAllTimers();
    this.removeUserActivityListeners();
    this.dialog.close();
  }
  openDialog() {
    if (!this.dialog) return;
    this.dialog.showModal();
    this.startDialogTimer();
    this.addUserActivityListeners();
  }
  startDialogTimer() {
    this.clearAllTimers();
    const countdownStartTime = this.dialogTimeOutSeconds - this.dialogVisibleTimerSeconds;
    this.countdownStartTimer = window.setTimeout(() => {
      this.startCountdown();
    }, countdownStartTime);
    this.dialogTimer = window.setTimeout(() => {
      this.dialog.close();
      this.clearAllTimers();
    }, this.dialogTimeOutSeconds);
  }
  startCountdown() {
    this.showCountdown = true;
    const startTime = performance.now();
    const totalDuration = this.dialogVisibleTimerSeconds;
    const updateCountdown = (currentTime) => {
      const elapsed = currentTime - startTime;
      const remaining = Math.max(0, totalDuration - elapsed);
      this.countdownSeconds = remaining / 1e3;
      if (remaining <= 0) {
        this.clearAllTimers();
        return;
      }
      this.countdownTimer = requestAnimationFrame(updateCountdown);
    };
    this.countdownTimer = requestAnimationFrame(updateCountdown);
  }
  clearAllTimers() {
    if (this.dialogTimer) {
      clearTimeout(this.dialogTimer);
      this.dialogTimer = void 0;
    }
    if (this.countdownTimer) {
      cancelAnimationFrame(this.countdownTimer);
      this.countdownTimer = void 0;
    }
    if (this.countdownStartTimer) {
      clearTimeout(this.countdownStartTimer);
      this.countdownStartTimer = void 0;
    }
    this.showCountdown = false;
    this.countdownSeconds = 0;
  }
  getProgressPercentage() {
    const totalSeconds = this.dialogVisibleTimerSeconds / 1e3;
    const progress = this.countdownSeconds / totalSeconds * 100;
    return Math.max(0, progress);
  }
  addUserActivityListeners() {
    this.userActivityHandler = () => {
      this.resetDialogTimer();
    };
    window.addEventListener("mousemove", this.userActivityHandler);
    window.addEventListener("touchstart", this.userActivityHandler);
    window.addEventListener("touchmove", this.userActivityHandler);
    window.addEventListener("keydown", this.userActivityHandler);
  }
  removeUserActivityListeners() {
    if (this.userActivityHandler) {
      window.removeEventListener("mousemove", this.userActivityHandler);
      window.removeEventListener("touchstart", this.userActivityHandler);
      window.removeEventListener("touchmove", this.userActivityHandler);
      window.removeEventListener("keydown", this.userActivityHandler);
      this.userActivityHandler = void 0;
    }
  }
  resetDialogTimer() {
    this.clearAllTimers();
    this.startDialogTimer();
  }
  disconnectedCallback() {
    super.disconnectedCallback();
    this.clearAllTimers();
    this.removeUserActivityListeners();
  }
};
ObcCard.styles = unsafeCSS(compentStyle);
__decorateClass([
  property({ type: Boolean, attribute: false })
], ObcCard.prototype, "showTitle", 2);
__decorateClass([
  property({ type: Boolean })
], ObcCard.prototype, "hasDialog", 2);
__decorateClass([
  property({ type: Number })
], ObcCard.prototype, "dialogTimeOutSeconds", 2);
__decorateClass([
  property({ type: Number })
], ObcCard.prototype, "dialogVisibleTimerSeconds", 2);
__decorateClass([
  query("dialog")
], ObcCard.prototype, "dialog", 2);
__decorateClass([
  state()
], ObcCard.prototype, "countdownSeconds", 2);
__decorateClass([
  state()
], ObcCard.prototype, "showCountdown", 2);
ObcCard = __decorateClass([
  customElement("obc-card")
], ObcCard);
export {
  ObcCard
};
//# sourceMappingURL=card.js.map
