"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.keyAclBody = exports.keyContainerAclBody = void 0;
exports.setAcl = setAcl;
const debug = __importStar(require("../../debug"));
const solid_logic_1 = require("solid-logic");
/**
 * set ACL
 * @param keyDoc
 * @param aclBody
 */
async function setAcl(keyDoc, aclBody) {
    // Some servers don't present a Link http response header
    // if the container doesn't exist yet, refetch the resource
    await solid_logic_1.store.fetcher.load(keyDoc);
    // FIXME: check the Why value on this quad:
    // debug.log(store.statementsMatching(store.sym(keyDoc), store.sym('http://www.iana.org/assignments/link-relations/acl')))
    const keyAclDoc = solid_logic_1.store.any(solid_logic_1.store.sym(keyDoc), solid_logic_1.store.sym('http://www.iana.org/assignments/link-relations/acl'));
    if (!keyAclDoc) {
        throw new Error('Key ACL doc not found!');
    }
    try {
        await solid_logic_1.store.fetcher.webOperation('PUT', keyAclDoc.value, {
            data: aclBody,
            contentType: 'text/turtle'
        });
    }
    catch (err) {
        if (err?.response?.status !== 404) {
            throw new Error(err);
        }
        debug.log('delete ' + keyAclDoc.value + ' ' + err.response.status); // should test 404 and 2xx
    }
}
/**
 * key container ACL
 * @param me
 * @returns aclBody
 */
const keyContainerAclBody = (me) => {
    const aclBody = `
@prefix : <#>.
@prefix acl: <http://www.w3.org/ns/auth/acl#>.
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
@prefix key: <./>.

:ReadWrite
    a acl:Authorization;
    acl:accessTo key:;
    acl:default key:;
    acl:agent <${me}>;
    acl:mode acl:Read, acl:Write.
`;
    return aclBody;
};
exports.keyContainerAclBody = keyContainerAclBody;
/**
 * Read only ACL
 * @param keyDoc
 * @param me
 * @returns aclBody
 */
const keyAclBody = (keyDoc, me) => {
    let keyAgent = 'acl:agentClass foaf:Agent'; // publicKey
    if (me?.length)
        keyAgent = `acl:agent <${me}>`; // privateKey
    const aclBody = `
@prefix foaf: <http://xmlns.com/foaf/0.1/>.
@prefix acl: <http://www.w3.org/ns/auth/acl#>.
<#Read>
    a acl:Authorization;
    ${keyAgent};
    acl:accessTo <${keyDoc.split('/').pop()}>;
    acl:mode acl:Read.
`;
    return aclBody;
};
exports.keyAclBody = keyAclBody;
//# sourceMappingURL=acl.js.map