import {
  __decorateClass,
  __privateAdd,
  __privateGet,
  __privateMethod,
  __privateSet
} from "../../chunks/chunk.QU3DSPNU.js";
var _timeoutId, _progressIntervalId, _alertRef, _observer, _isVisible, _remaining, _lastTick, _timerController, _GdsAlert_instances, setupObserver_fn, disconnectObserver_fn, startTicking_fn, resumeTimer_fn, pauseTimer_fn, clearTimers_fn, dismiss_fn, handleButtonClick_fn, handleKeyDown_fn, config_get, renderIcon_fn, renderMessage_fn, renderActionButton_fn, renderCloseButton_fn, renderProgressBar_fn;
import { localized, msg } from "@lit/localize";
import { nothing } from "lit";
import { property, state } from "lit/decorators.js";
import { classMap } from "lit/directives/class-map.js";
import { createRef, ref } from "lit/directives/ref.js";
import { html as staticHtml, unsafeStatic } from "lit/static-html.js";
import { GdsElement } from "../../gds-element.js";
import { gdsCustomElement, getScopedTagName, html } from "../../scoping.js";
import { tokens } from "../../tokens.style.js";
import { GdsButton } from "../button/button.component.js";
import { GdsCard } from "../card/card.component.js";
import { IconCircleCheck } from "../icon/icons/circle-check.component.js";
import { IconCircleInfo } from "../icon/icons/circle-info.component.js";
import { IconCrossSmall } from "../icon/icons/cross-small.component.js";
import { IconTriangleExclamation } from "../icon/icons/triangle-exclamation.component.js";
import AlertStyles from "./alert.styles.js";
const VARIANT_CONFIG = {
  positive: { icon: "circle-check", card: "positive" },
  warning: {
    icon: "circle-info",
    card: "warning"
  },
  negative: {
    icon: "triangle-exclamation",
    card: "negative"
  },
  information: {
    icon: "circle-info",
    card: "information"
  },
  notice: { icon: "circle-info", card: "notice" }
};
const FADE_DURATION = 300;
const PROGRESS_INTERVAL = 100;
let GdsAlert = class extends GdsElement {
  constructor() {
    super();
    __privateAdd(this, _GdsAlert_instances);
    this.variant = "information";
    this.label = "";
    this.role = "alert";
    this.dismissible = false;
    this.timeout = 0;
    this.buttonLabel = "";
    this._progress = 100;
    this._isClosing = false;
    this._cardHidden = false;
    __privateAdd(this, _timeoutId);
    __privateAdd(this, _progressIntervalId);
    __privateAdd(this, _alertRef, createRef());
    __privateAdd(this, _observer);
    __privateAdd(this, _isVisible, false);
    __privateAdd(this, _remaining, 0);
    __privateAdd(this, _lastTick, 0);
    __privateAdd(this, _timerController, {
      hostConnected: () => {
        if (this.timeout > 0) {
          __privateMethod(this, _GdsAlert_instances, setupObserver_fn).call(this);
        }
      },
      hostDisconnected: () => {
        __privateMethod(this, _GdsAlert_instances, disconnectObserver_fn).call(this);
        __privateMethod(this, _GdsAlert_instances, clearTimers_fn).call(this);
      }
    });
    this.addController(__privateGet(this, _timerController));
  }
  updated(changed) {
    if (changed.has("timeout")) {
      __privateMethod(this, _GdsAlert_instances, disconnectObserver_fn).call(this);
      __privateMethod(this, _GdsAlert_instances, clearTimers_fn).call(this);
      __privateSet(this, _remaining, Math.max(0, this.timeout));
      this._progress = 100;
      if (this.timeout > 0) __privateMethod(this, _GdsAlert_instances, setupObserver_fn).call(this);
    }
  }
  render() {
    if (this._cardHidden) return nothing;
    const classes = {
      dismissing: this._isClosing,
      dismissible: this.dismissible,
      "has-action": this.buttonLabel,
      timeout: this.timeout > 0
    };
    return html`
      <gds-card
        ${ref(__privateGet(this, _alertRef))}
        role=${this.role}
        aria-label=${this.label}
        variant=${__privateGet(this, _GdsAlert_instances, config_get).card}
        class=${classMap(classes)}
        @keydown=${__privateMethod(this, _GdsAlert_instances, handleKeyDown_fn)}
        id="alert-message"
        padding="m"
      >
        <div class="wrapper">
          ${__privateMethod(this, _GdsAlert_instances, renderIcon_fn).call(this)} ${__privateMethod(this, _GdsAlert_instances, renderMessage_fn).call(this)}
        </div>
        ${!this.timeout && this.buttonLabel ? __privateMethod(this, _GdsAlert_instances, renderActionButton_fn).call(this, this.buttonLabel) : nothing}
        ${__privateMethod(this, _GdsAlert_instances, renderCloseButton_fn).call(this)} ${__privateMethod(this, _GdsAlert_instances, renderProgressBar_fn).call(this)}
      </gds-card>
    `;
  }
};
_timeoutId = new WeakMap();
_progressIntervalId = new WeakMap();
_alertRef = new WeakMap();
_observer = new WeakMap();
_isVisible = new WeakMap();
_remaining = new WeakMap();
_lastTick = new WeakMap();
_timerController = new WeakMap();
_GdsAlert_instances = new WeakSet();
setupObserver_fn = function() {
  if (__privateGet(this, _observer)) return;
  if (!__privateGet(this, _remaining)) __privateSet(this, _remaining, Math.max(0, this.timeout));
  __privateSet(this, _observer, new IntersectionObserver(
    (entries) => {
      const entry = entries[0];
      const ratio = entry?.intersectionRatio ?? 0;
      const nowVisible = ratio >= 0.1;
      if (nowVisible && !__privateGet(this, _isVisible)) {
        __privateSet(this, _isVisible, true);
        __privateMethod(this, _GdsAlert_instances, resumeTimer_fn).call(this);
      } else if (!nowVisible && __privateGet(this, _isVisible)) {
        __privateSet(this, _isVisible, false);
        __privateMethod(this, _GdsAlert_instances, pauseTimer_fn).call(this);
      }
    },
    {
      root: null,
      threshold: [0, 0.1, 1]
    }
  ));
  __privateGet(this, _observer).observe(this);
};
disconnectObserver_fn = function() {
  if (__privateGet(this, _observer)) {
    __privateGet(this, _observer).disconnect();
    __privateSet(this, _observer, void 0);
  }
  __privateSet(this, _isVisible, false);
};
// Timer management (pausable)
startTicking_fn = function() {
  __privateSet(this, _lastTick, Date.now());
  __privateSet(this, _progressIntervalId, window.setInterval(() => {
    const now = Date.now();
    const dt = now - __privateGet(this, _lastTick);
    __privateSet(this, _lastTick, now);
    __privateSet(this, _remaining, Math.max(0, __privateGet(this, _remaining) - dt));
    this._progress = this.timeout > 0 ? Math.max(0, __privateGet(this, _remaining) / this.timeout * 100) : 0;
    if (__privateGet(this, _remaining) <= 0) {
      __privateMethod(this, _GdsAlert_instances, dismiss_fn).call(this, "timeout");
    }
  }, PROGRESS_INTERVAL));
};
resumeTimer_fn = function() {
  if (__privateGet(this, _remaining) <= 0 || __privateGet(this, _timeoutId) || __privateGet(this, _progressIntervalId))
    return;
  __privateMethod(this, _GdsAlert_instances, startTicking_fn).call(this);
};
pauseTimer_fn = function() {
  __privateMethod(this, _GdsAlert_instances, clearTimers_fn).call(this);
};
clearTimers_fn = function() {
  clearTimeout(__privateGet(this, _timeoutId));
  clearInterval(__privateGet(this, _progressIntervalId));
  __privateSet(this, _timeoutId, __privateSet(this, _progressIntervalId, void 0));
};
dismiss_fn = async function(source) {
  this._isClosing = true;
  __privateMethod(this, _GdsAlert_instances, disconnectObserver_fn).call(this);
  __privateMethod(this, _GdsAlert_instances, clearTimers_fn).call(this);
  await this.updateComplete;
  this.dispatchCustomEvent("gds-close", {
    detail: { source },
    bubbles: true,
    composed: true
  });
  await new Promise((r) => setTimeout(r, FADE_DURATION));
  this._cardHidden = true;
};
// Event handlers
handleButtonClick_fn = function(e) {
  this.dispatchCustomEvent("gds-action", {
    detail: { source: "button", event: e },
    bubbles: true,
    composed: true
  });
};
handleKeyDown_fn = function(e) {
  if (e.key === "Escape" && this.dismissible) {
    e.preventDefault();
    __privateMethod(this, _GdsAlert_instances, dismiss_fn).call(this, "escape");
  }
};
config_get = function() {
  return VARIANT_CONFIG[this.variant];
};
// Render methods
renderIcon_fn = function() {
  const icon = `gds-icon-${__privateGet(this, _GdsAlert_instances, config_get).icon}`;
  return html`${staticHtml`<${unsafeStatic(getScopedTagName(icon))} class="icon" solid aria-hidden="true" size="24px"></${unsafeStatic(getScopedTagName(icon))}>`}`;
};
renderMessage_fn = function() {
  return html`<div class="message">
      <span class="message-text">
        <slot></slot>
      </span>
    </div>`;
};
renderActionButton_fn = function(label) {
  return html`
      <gds-button
        class="action"
        .variant=${this.variant}
        rank="secondary"
        @click=${__privateMethod(this, _GdsAlert_instances, handleButtonClick_fn)}
        aria-describedby="alert-message"
      >
        ${label.trim()}
      </gds-button>
    `;
};
renderCloseButton_fn = function() {
  return this.dismissible ? html`
          <gds-button
            class="close"
            .variant=${this.variant}
            rank="secondary"
            size="small"
            aria-label=${msg("Dismiss alert")}
            @click=${() => __privateMethod(this, _GdsAlert_instances, dismiss_fn).call(this, "close")}
          >
            <gds-icon-cross-small size="20px"></gds-icon-cross-small>
          </gds-button>
        ` : nothing;
};
renderProgressBar_fn = function() {
  return this.timeout > 0 ? html`
          <div
            class=${classMap({
    "progress-container": true,
    [this.variant]: true
  })}
            role="timer"
            aria-label=${msg("Auto-dismiss timer")}
            aria-valuenow=${this._progress}
            aria-valuemin="0"
            aria-valuemax="100"
          >
            <div class="timer-progress" style="width: ${this._progress}%"></div>
          </div>
        ` : nothing;
};
GdsAlert.styles = [tokens, AlertStyles];
__decorateClass([
  property({ type: String, reflect: true })
], GdsAlert.prototype, "variant", 2);
__decorateClass([
  property({ type: String })
], GdsAlert.prototype, "label", 2);
__decorateClass([
  property({ type: String, reflect: true })
], GdsAlert.prototype, "role", 2);
__decorateClass([
  property({ type: Boolean })
], GdsAlert.prototype, "dismissible", 2);
__decorateClass([
  property({ type: Number })
], GdsAlert.prototype, "timeout", 2);
__decorateClass([
  property({ type: String, attribute: "button-label" })
], GdsAlert.prototype, "buttonLabel", 2);
__decorateClass([
  state()
], GdsAlert.prototype, "_progress", 2);
__decorateClass([
  state()
], GdsAlert.prototype, "_isClosing", 2);
__decorateClass([
  state()
], GdsAlert.prototype, "_cardHidden", 2);
GdsAlert = __decorateClass([
  gdsCustomElement("gds-alert", {
    dependsOn: [
      GdsButton,
      GdsCard,
      IconCircleCheck,
      IconTriangleExclamation,
      IconCircleInfo,
      IconCrossSmall
    ]
  }),
  localized()
], GdsAlert);
export {
  GdsAlert
};
