// /AIWK_COMMON/loader/aiwk_css_loader.js
// AIWK EXT4 v056_css_load_nonfatal
(function () {
    "use strict";

    const AIWK = window.AIWK;

    AIWK.cssLoader = {
        load: function (path, id) {
            return new Promise(function (resolve, reject) {
                if (id && document.getElementById(id)) {
                    resolve(document.getElementById(id));
                    return;
                }
                const link = document.createElement("link");
                link.rel = "stylesheet";
                link.href = AIWK.assetLoader.url(path);
                if (id) {
                    link.id = id;
                }
                link.onload = function () {
                    resolve(link);
                };
                link.onerror = function () {
                    const err = new Error("CSS load failed: " + path);
                    try {
                        console.warn("AIWK CSS load warning", err);
                        AIWK.cssLoader.lastErrors = AIWK.cssLoader.lastErrors || [];
                        AIWK.cssLoader.lastErrors.push({ path: path, message: err.message, at: new Date().toISOString() });
                    } catch (e) {}
                    resolve(null);
                };
                document.documentElement.appendChild(link);
            });
        }
    };
}());
