import { html } from 'lit';
import { property, state } from 'lit/decorators.js';
import { styleMap } from 'lit/directives/style-map.js';
import { tv } from 'tailwind-variants';
import { T as TailwindElement, s as safeCustomElement } from '../../chunks/index-DluTRl1F.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 LuksoImage = class extends TailwindElement {
  constructor() {
    super(...arguments);
    this.src = "";
    this.placeholder = "";
    this.isLoading = true;
    this.resolvedPlaceholder = "";
    this.hasError = false;
    this.imageStyles = tv({
      slots: {
        wrapper: "size-full bg-neutral-100",
        placeholder: "bg-[50%] bg-no-repeat bg-cover bg-neutral-90 relative size-full",
        image: "object-cover opacity-0 size-full"
      },
      variants: {
        isLoading: {
          true: {}
        },
        hasError: {
          true: {}
        }
      },
      compoundVariants: [
        {
          isLoading: false,
          hasError: false,
          class: {
            image: "animate-fade-in animation-fill-forwards"
          }
        },
        {
          isLoading: true,
          hasError: false,
          class: {
            placeholder: "animate-pulse"
          }
        },
        {
          isLoading: false,
          hasError: true,
          class: {
            image: "opacity-0"
          }
        }
      ]
    });
  }
  handleLoad() {
    this.isLoading = false;
    this.hasError = false;
  }
  handleError() {
    this.isLoading = false;
    this.hasError = true;
  }
  async willUpdate(changedProperties) {
    if (changedProperties.has("hasError")) {
      if (this.hasError) {
        this.resolvedPlaceholder = this.placeholder;
      } else {
        this.resolvedPlaceholder = "";
      }
      await this.updateComplete;
    }
  }
  render() {
    const { wrapper, placeholder, image } = this.imageStyles({
      isLoading: this.isLoading,
      hasError: this.hasError
    });
    return html`
      <div class=${wrapper()}>
        <div
          class=${placeholder()}
          style=${styleMap({
      backgroundImage: `url('${this.resolvedPlaceholder}')`
    })}
        >
          <img
            src=${this.src}
            class=${image()}
            loading="lazy"
            alt=""
            @load=${this.handleLoad}
            @error=${this.handleError}
          />
        </div>
      </div>
    `;
  }
};
__decorateClass([
  property({ type: String, reflect: true })
], LuksoImage.prototype, "src", 2);
__decorateClass([
  property({ type: String })
], LuksoImage.prototype, "placeholder", 2);
__decorateClass([
  state()
], LuksoImage.prototype, "isLoading", 2);
__decorateClass([
  state()
], LuksoImage.prototype, "resolvedPlaceholder", 2);
__decorateClass([
  state()
], LuksoImage.prototype, "hasError", 2);
LuksoImage = __decorateClass([
  safeCustomElement("lukso-image")
], LuksoImage);

export { LuksoImage };
