"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.createSolidLogic = createSolidLogic;
const rdf = __importStar(require("rdflib"));
const aclLogic_1 = require("../acl/aclLogic");
const SolidAuthnLogic_1 = require("../authn/SolidAuthnLogic");
const chatLogic_1 = require("../chat/chatLogic");
const inboxLogic_1 = require("../inbox/inboxLogic");
const profileLogic_1 = require("../profile/profileLogic");
const typeIndexLogic_1 = require("../typeIndex/typeIndexLogic");
const containerLogic_1 = require("../util/containerLogic");
const utilityLogic_1 = require("../util/utilityLogic");
const debug = __importStar(require("../util/debug"));
/*
** It is important to distinquish `fetch`, a function provided by the browser
** and `Fetcher`, a helper object for the rdflib Store which turns it
** into a `ConnectedStore` or a `LiveStore`.  A Fetcher object is
** available at store.fetcher, and `fetch` function at `store.fetcher._fetch`,
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function createSolidLogic(specialFetch, session) {
    debug.log('SolidLogic: Unique instance created.  There should only be one of these.');
    const store = rdf.graph();
    rdf.fetcher(store, { fetch: specialFetch.fetch }); // Attach a web I/O module, store.fetcher
    store.updater = new rdf.UpdateManager(store); // Add real-time live updates store.updater
    store.features = []; // disable automatic node merging on store load
    const authn = new SolidAuthnLogic_1.SolidAuthnLogic(session);
    const acl = (0, aclLogic_1.createAclLogic)(store);
    const containerLogic = (0, containerLogic_1.createContainerLogic)(store);
    const utilityLogic = (0, utilityLogic_1.createUtilityLogic)(store, acl, containerLogic);
    const profile = (0, profileLogic_1.createProfileLogic)(store, authn, utilityLogic);
    const chat = (0, chatLogic_1.createChatLogic)(store, profile);
    const inbox = (0, inboxLogic_1.createInboxLogic)(store, profile, utilityLogic, containerLogic, acl);
    const typeIndex = (0, typeIndexLogic_1.createTypeIndexLogic)(store, authn, profile, utilityLogic);
    debug.log('SolidAuthnLogic initialized');
    function load(doc) {
        return store.fetcher.load(doc);
    }
    // @@@@ use the one in rdflib.js when it is available and delete this
    function updatePromise(del, ins = []) {
        return new Promise((resolve, reject) => {
            store.updater.update(del, ins, function (_uri, ok, errorBody) {
                if (!ok) {
                    reject(new Error(errorBody));
                }
                else {
                    resolve();
                }
            }); // callback
        }); // promise
    }
    function clearStore() {
        store.statements.slice().forEach(store.remove.bind(store));
    }
    return {
        store,
        authn,
        acl,
        inbox,
        chat,
        profile,
        typeIndex,
        load,
        updatePromise,
        clearStore
    };
}
//# sourceMappingURL=solidLogic.js.map