"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.azartIdentityAdapter = exports.azartIdentity = void 0;
const tslib_1 = require("tslib");
const codiac_relay_contracts_1 = require("@codiac.io/codiac-relay-contracts");
class azartIdentity extends codiac_relay_contracts_1.TokenizedIdentity {
    constructor(provider, registryUrl, token, organization, feed, email, scope, tokenName) {
        super(provider, registryUrl, token, scope, tokenName);
        this.organization = organization;
        this.feed = feed;
        this.email = email;
    }
}
exports.azartIdentity = azartIdentity;
class azartIdentityAdapter {
    constructor(_projectRoot, logger) {
        this._projectRoot = _projectRoot;
        this.logger = logger;
        this.provider = codiac_relay_contracts_1.IdentityProvider.azart;
    }
    createIdentity(token, input) {
        if (!input.organization || !input.feed || !input.email) {
            throw new Error(`Incomplete input: a ${this.provider} identity requires entries for organization, feed, and email.`);
        }
        let regUrl = input.registryUrl || this.buildDefaultRegistryUrl(input.organization, input.feed);
        return new azartIdentity(this.provider, regUrl, token, input.organization, input.feed, input.email, input.scope, input.tokenName);
    }
    buildDefaultRegistryUrl(organization, feed) {
        return `pkgs.dev.azure.com/${organization}/_packaging/${feed}/npm/registry/`;
    }
    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 {
                    // from:  https://dev.azure.com/codiacdevops/Codiac/_packaging?_a=connect&feed=codiacdevops
                    //
                    // USER npmrc:
                    // pkgs.dev.azure.com/codiacdevops/_packaging/codiacdevops/npm/registry/:username=codiacdevops
                    // pkgs.dev.azure.com/codiacdevops/_packaging/codiacdevops/npm/registry/:_password=[BASE64_ENCODED_PERSONAL_ACCESS_TOKEN]
                    // pkgs.dev.azure.com/codiacdevops/_packaging/codiacdevops/npm/registry/:email=npm requires email to be set but doesn't use the value
                    // pkgs.dev.azure.com/codiacdevops/_packaging/codiacdevops/npm/:username=codiacdevops
                    // pkgs.dev.azure.com/codiacdevops/_packaging/codiacdevops/npm/:_password=[BASE64_ENCODED_PERSONAL_ACCESS_TOKEN]
                    // pkgs.dev.azure.com/codiacdevops/_packaging/codiacdevops/npm/:email=npm requires email to be set but doesn't use the value
                    // PROJECT npmrc:
                    // registry=https://pkgs.dev.azure.com/codiacdevops/_packaging/codiacdevops/npm/registry/
                    // always-auth=true
                    // ...but from https://docs.microsoft.com/en-us/azure/devops/artifacts/get-started-npm?view=azure-devops&tabs=mac
                    // USER npmrc:
                    // pkgs.dev.azure.com/<yourorganization>/_packaging/<yourfeed>/npm/registry/:username=ANYTHING-BUT-EMPTY
                    // pkgs.dev.azure.com/<yourorganization>/_packaging/<yourfeed>/npm/registry/:_password=BASE64-ENCODED-PAT-GOES-HERE
                    // pkgs.dev.azure.com/<yourorganization>/_packaging/<yourfeed>/npm/registry/:email=YOUREMAIL@EXAMPLE.COM
                    // pkgs.dev.azure.com/<yourorganization>/_packaging/<yourfeed>/npm/registry/:always-auth=true
                    let ours = identities.filter(x => x.provider == this.provider);
                    let overridingDefaultRegistry = false;
                    let lastDefaultReg = "";
                    let npmrcEntries = [];
                    for (let index in ours) {
                        let identity = ours[index];
                        let registryUrl = identity.registryUrl;
                        // Proj-user-level
                        let projUserEntries = {};
                        let tokenSetting = `${registryUrl}:_password`;
                        projUserEntries[tokenSetting] = identity.token;
                        projUserEntries[`${registryUrl}:username`] = identity.organization;
                        projUserEntries[`${registryUrl}:email`] = identity.email;
                        // 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";
                            // This will knock npmjs off the list.  
                            // If they need both, one of them will need to operate from a scope.
                            overridingDefaultRegistry = true;
                            lastDefaultReg = registryUrl;
                        }
                        npmrcEntries.push({
                            identity: identity,
                            tokenSetting: tokenSetting,
                            projUserEntries: projUserEntries,
                            projEntries: projEntries
                        });
                    }
                    resolve(npmrcEntries);
                }
                catch (err) {
                    reject(err);
                }
            }));
        });
    }
}
exports.azartIdentityAdapter = azartIdentityAdapter;
//# sourceMappingURL=azart-identity-adapter.js.map