// retoor <retoor@molodetz.nl>

import { assetUrl } from "../assetVersion.js";

const COMP_CSS_ID = "dp-components-css";

export class Component extends HTMLElement {
    constructor() {
        super();
        this._ensureCss();
    }

    _ensureCss() {
        if (document.getElementById(COMP_CSS_ID)) return;
        const link = document.createElement("link");
        link.id = COMP_CSS_ID;
        link.rel = "stylesheet";
        link.href = assetUrl("/static/css/components.css");
        document.head.appendChild(link);
    }

    attr(name, fallback = "") {
        const value = this.getAttribute(name);
        return value === null ? fallback : value;
    }

    boolAttr(name) {
        return this.hasAttribute(name);
    }

    intAttr(name, fallback = 0) {
        const value = parseInt(this.getAttribute(name), 10);
        return Number.isNaN(value) ? fallback : value;
    }
}
