"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.npmjsIdentityAdapter = void 0;
const tslib_1 = require("tslib");
const codiac_relay_contracts_1 = require("@codiac.io/codiac-relay-contracts");
class npmjsIdentityAdapter {
    constructor(_projectRoot, logger) {
        this._projectRoot = _projectRoot;
        this.logger = logger;
        this.provider = codiac_relay_contracts_1.IdentityProvider.npm;
        this.DEFAULT_REGISTRY_URL = "http://registry.npmjs.org/";
    }
    createIdentity(token, input) {
        let regUrl = input.registryUrl || this.DEFAULT_REGISTRY_URL;
        let identity = new codiac_relay_contracts_1.TokenizedIdentity(this.provider, regUrl, token, input.scope, input.tokenName);
        return identity;
    }
    syncIdentities(identities) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return new Promise((resolve, reject) => tslib_1.__awaiter(this, void 0, void 0, function* () {
                try {
                    let npmrcEntries = [];
                    let ours = identities.filter(x => x.provider == this.provider);
                    for (let index in ours) {
                        let identity = ours[index];
                        let registryUrl = identity.registryUrl;
                        // Proj-user-level
                        let projUserEntries = {};
                        // IMPORTANT!!  This key (the name of the variable we're inserting) is the registry url with the http segment removed !!!
                        let regKey = registryUrl;
                        if (registryUrl.indexOf("://") > 0) {
                            regKey = registryUrl.substr(registryUrl.indexOf("://") + 1);
                            // regKey = registryUrl.split(":")!.splice(0,1).join(":");
                        }
                        let tokenSetting = `${regKey}:_authToken`;
                        projUserEntries[tokenSetting] = identity.token;
                        // Proj-level
                        let projEntries = {};
                        if (identity.scope) {
                            projEntries[`${identity.scope}:registry`] = registryUrl;
                            projEntries[`${identity.scope}:always-auth`] = "true";
                        }
                        else {
                            projEntries["registry"] = registryUrl;
                            projEntries["always-auth"] = "true";
                        }
                        npmrcEntries.push({
                            identity: identity,
                            tokenSetting: tokenSetting,
                            projUserEntries: projUserEntries,
                            projEntries: projEntries
                        });
                    }
                    resolve(npmrcEntries);
                }
                catch (err) {
                    reject(err);
                }
            }));
        });
    }
}
exports.npmjsIdentityAdapter = npmjsIdentityAdapter;
//# sourceMappingURL=npmjs-identity-adapter.js.map