"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createInboxLogic = createInboxLogic;
const utils_1 = require("../util/utils");
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function createInboxLogic(store, profileLogic, utilityLogic, containerLogic, aclLogic) {
    async function createInboxFor(peerWebId, nick) {
        const myWebId = await profileLogic.loadMe();
        const podRoot = await profileLogic.getPodRoot(myWebId);
        const ourInbox = `${podRoot.value}p2p-inboxes/${encodeURIComponent(nick)}/`;
        await containerLogic.createContainer(ourInbox);
        // const aclDocUrl = await aclLogic.findAclDocUrl(ourInbox);
        await utilityLogic.setSinglePeerAccess({
            ownerWebId: myWebId.value,
            peerWebId,
            accessToModes: 'acl:Append',
            target: ourInbox
        });
        return ourInbox;
    }
    async function getNewMessages(user) {
        if (!user) {
            user = await profileLogic.loadMe();
        }
        const inbox = await profileLogic.getMainInbox(user);
        const urls = await containerLogic.getContainerMembers(inbox);
        return urls.filter(url => !containerLogic.isContainer(url));
    }
    async function markAsRead(url, date) {
        const downloaded = await store.fetcher._fetch(url);
        if (downloaded.status !== 200) {
            throw new Error(`Not OK! ${url}`);
        }
        const archiveUrl = (0, utils_1.getArchiveUrl)(url, date);
        const options = {
            method: 'PUT',
            body: await downloaded.text(),
            headers: [
                ['Content-Type', downloaded.headers.get('Content-Type') || 'application/octet-stream']
            ]
        };
        const uploaded = await store.fetcher._fetch(archiveUrl, options);
        if (uploaded.status.toString()[0] === '2') {
            await store.fetcher._fetch(url, {
                method: 'DELETE'
            });
        }
    }
    return {
        createInboxFor,
        getNewMessages,
        markAsRead
    };
}
//# sourceMappingURL=inboxLogic.js.map