export default class PreviewContentDialog extends HTMLSpanElement {
    #url;
    #service;
    #status;

    #statusTexts = {
        200: 'up',
        204: 'up'
    }

    #statusColors = {
        200: 'bg-green-500',
        204: 'bg-green-500'
    }

    constructor() {
        super();
        this.#url = this.getAttribute('url');
        this.#service = this.getAttribute('service');

        (async () => await this.checkStatus())();
        setInterval(async () => await this.checkStatus(), 1000 * 60 * 10);
    }

    async checkStatus() {
        const response = await fetch(this.#url);
        this.#status = response.status;
        this.setAttribute('title', this.getTitle());
        this.classList.remove('bg-red-500', 'bg-green-500');
        this.classList.add(this.getColor());
    }

    getTitle() {
        return `${this.#service} is ${this.#statusTexts[this.#status] ?? 'down'}`
    }

    getColor() {
        return this.#statusColors[this.#status] ?? 'bg-red-500';
    }
}
