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 * as debug from '../../debug';
import { store } from 'solid-logic';
import ns from '../../ns';
import { getRootIfPreferencesExist } from './otherHelpers';
export const pubKeyUrl = (webId) => {
    let url;
    try {
        const root = getRootIfPreferencesExist(webId);
        url = `${root}/profile/keys/publicKey.ttl`;
    }
    catch (err) {
        debug.error(err);
    }
    return url;
};
export const privKeyUrl = (webId) => {
    let url;
    try {
        const root = getRootIfPreferencesExist(webId);
        url = `${root}/settings/keys/privateKey.ttl`;
    }
    catch (err) {
        debug.error(err);
    }
    return url;
};
export function getExistingPublicKey(webId, publicKeyUrl) {
    return __awaiter(this, void 0, void 0, function* () {
        // find publickey
        return yield getKeyIfExists(webId, publicKeyUrl, 'publicKey');
    });
}
export function getExistingPrivateKey(webId, privateKeyUrl) {
    return __awaiter(this, void 0, void 0, function* () {
        // find privateKey
        return yield getKeyIfExists(webId, privateKeyUrl, 'privateKey');
    });
}
export function getKeyIfExists(webId, keyUrl, keyType) {
    return __awaiter(this, void 0, void 0, function* () {
        try {
            yield store.fetcher.load(keyUrl);
            const key = store.any(webId, ns.solid(keyType)); // store.sym(CERT + keyType))
            return key === null || key === void 0 ? void 0 : key.value; // as NamedNode
        }
        catch (err) {
            if (err.response.status === 404) {
                debug.log('createIfNotExists: doc does NOT exist, will create... ' + keyUrl);
                try {
                    yield store.fetcher.webOperation('PUT', keyUrl, {
                        data: '',
                        contentType: 'text/turtle'
                    });
                }
                catch (err) {
                    debug.log('createIfNotExists doc FAILED: ' + keyUrl + ': ' + err);
                    throw err;
                }
                delete store.fetcher.requested[keyUrl]; // delete cached 404 error
                // debug.log('createIfNotExists doc created ok ' + doc)
                return undefined; // response
            }
            else {
                debug.log('createIfNotExists doc FAILED: ' + keyUrl + ': ' + err);
                throw err;
            }
        }
    });
}
//# sourceMappingURL=accessData.js.map