"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.appContext = void 0;
exports.offlineTestID = offlineTestID;
const rdflib_1 = require("rdflib");
const debug = __importStar(require("../util/debug"));
/**
 * find a user or app's context as set in window.SolidAppContext
 * this is a const, not a function, because we have problems to jest mock it otherwise
 * see: https://github.com/facebook/jest/issues/936#issuecomment-545080082 for more
 * @return {any} - an appContext object
 */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const appContext = () => {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    let { SolidAppContext } = window;
    SolidAppContext ||= {};
    SolidAppContext.viewingNoAuthPage = false;
    if (SolidAppContext.noAuth && window.document) {
        const currentPage = window.document.location.href;
        if (currentPage.startsWith(SolidAppContext.noAuth)) {
            SolidAppContext.viewingNoAuthPage = true;
            const params = new URLSearchParams(window.document.location.search);
            if (params) {
                let viewedPage = SolidAppContext.viewedPage = params.get('uri') || null;
                if (viewedPage) {
                    viewedPage = decodeURI(viewedPage);
                    if (!viewedPage.startsWith(SolidAppContext.noAuth)) {
                        const ary = viewedPage.split(/\//);
                        SolidAppContext.idp = ary[0] + '//' + ary[2];
                        SolidAppContext.viewingNoAuthPage = false;
                    }
                }
            }
        }
    }
    return SolidAppContext;
};
exports.appContext = appContext;
/**
 * Returns `sym($SolidTestEnvironment.username)` if
 * `$SolidTestEnvironment.username` is defined as a global
 * or
 * returns testID defined in the HTML page
 * @returns {NamedNode|null}
 */
function offlineTestID() {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any 
    const { $SolidTestEnvironment } = window;
    if (typeof $SolidTestEnvironment !== 'undefined' &&
        $SolidTestEnvironment.username) {
        // Test setup
        debug.log('Assuming the user is ' + $SolidTestEnvironment.username);
        return (0, rdflib_1.sym)($SolidTestEnvironment.username);
    }
    // hack that makes SolidOS work in offline mode by adding the webId directly in html
    // example usage: https://github.com/solidos/mashlib/blob/29b8b53c46bf02e0e219f0bacd51b0e9951001dd/test/contact/local.html#L37
    if (typeof document !== 'undefined' &&
        document.location &&
        ('' + document.location).slice(0, 16) === 'http://localhost') {
        const div = document.getElementById('appTarget');
        if (!div)
            return null;
        const id = div.getAttribute('testID');
        if (!id)
            return null;
        debug.log('Assuming user is ' + id);
        return (0, rdflib_1.sym)(id);
    }
    return null;
}
//# sourceMappingURL=authUtil.js.map