var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { authn, authSession } from 'solid-logic';
import { style } from '../style';
import { getName, getPod, getPodOwner } from '../utils/headerFooterHelpers';
const DEFAULT_SOLID_PROJECT_URL = 'https://solidproject.org';
const DEFAULT_SOLID_PROJECT_NAME = 'solidproject.org';
/**
 * Initialize footer component, the footer object returned depends on whether the user is authenticated.
 * @param store the data store
 * @returns the footer
 */
export function initFooter(store, options) {
    return __awaiter(this, void 0, void 0, function* () {
        const footer = document.getElementById('PageFooter');
        if (!footer) {
            return;
        }
        const pod = getPod();
        const podOwner = yield getPodOwner(pod, store);
        rebuildFooter(footer, store, pod, podOwner, options)();
        authSession.onLogin(rebuildFooter(footer, store, pod, podOwner, options));
        authSession.onLogout(rebuildFooter(footer, store, pod, podOwner, options));
    });
}
/**
 * @ignore exporting this only for the unit test
 */
export function rebuildFooter(footer, store, pod, podOwner, options) {
    return () => __awaiter(this, void 0, void 0, function* () {
        const user = authn.currentUser();
        footer.innerHTML = '';
        footer.appendChild(yield createControllerInfoBlock(store, user, pod, podOwner, options));
    });
}
/**
 * @ignore exporting this only for the unit test
 */
export function createControllerInfoBlock(store, user, pod, podOwner, options) {
    const profileLinkContainer = document.createElement('div');
    if (!pod || !podOwner || (user && user.equals(podOwner))) {
        return profileLinkContainer;
    }
    profileLinkContainer.setAttribute('style', style.footer);
    const podLinkPre = document.createElement('span');
    podLinkPre.innerText = 'You\'re visiting ';
    const podLink = document.createElement('a');
    podLink.href = pod.uri;
    podLink.innerText = 'the Pod';
    const profileLinkPre = document.createElement('span');
    profileLinkPre.innerText = ' controlled by ';
    const profileLink = document.createElement('a');
    profileLink.href = podOwner.uri;
    profileLink.innerText = getName(store, podOwner);
    const solidProjectLinkPre = document.createElement('span');
    solidProjectLinkPre.innerText = '. For more info, check out ';
    const solidProjectLink = document.createElement('a');
    solidProjectLink.href = options && options.solidProjectUrl ? options.solidProjectUrl : DEFAULT_SOLID_PROJECT_URL;
    solidProjectLink.innerText = options && options.solidProjectName ? options.solidProjectName : DEFAULT_SOLID_PROJECT_NAME;
    const solidProjectLinkPost = document.createElement('span');
    solidProjectLinkPost.innerText = '.';
    profileLinkContainer.appendChild(podLinkPre);
    profileLinkContainer.appendChild(podLink);
    profileLinkContainer.appendChild(profileLinkPre);
    profileLinkContainer.appendChild(profileLink);
    profileLinkContainer.appendChild(solidProjectLinkPre);
    profileLinkContainer.appendChild(solidProjectLink);
    profileLinkContainer.appendChild(solidProjectLinkPost);
    return profileLinkContainer;
}
//# sourceMappingURL=index.js.map