"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const _ = require("lodash");
const command_base_tenantless_1 = require("../../command-base-tenantless");
class PkgPatch extends command_base_tenantless_1.CommandBaseTenantless {
    run() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            this.bootstrap();
            const { args, flags } = yield this.parse(PkgPatch);
            let packageName = args.packageName;
            if (this._projCfg.packages && this._projCfg.packages[packageName]) {
                let pkg = this._projCfg.packages[packageName]; /* goddamn nullable dictionary */
                if (pkg) {
                    let exps = (flags.set) ? flags.set.split(" ") : [];
                    let unsets = (flags.unset) ? flags.unset.split(" ") : [];
                    if (exps.length == 0 && unsets.length == 0) {
                        this._ui.userMessage("No modifications were specified.  Use the --set and --unset arguments to apply changes.");
                        this._ui.userMessage("No work was done.");
                        this.exit();
                    }
                    else {
                        // Sets
                        if (exps.length > 0) {
                            let patchObj = {};
                            for (let index in exps) {
                                let path = exps[index].split("=")[0];
                                let val = exps[index].split("=")[1];
                                // TODO: Strip out any property assignments that dont conform to PackageExport
                                //       (consider throwing error instead of continuing)
                                patchObj = _.set(patchObj, path, val); //NOTE:  This doesnt retain native types
                                if (path == "port")
                                    patchObj.port = parseInt(val);
                            }
                            this._projCfg.packages[packageName] = _.assignIn(pkg, patchObj);
                            yield this.projCfgRepo.updateProjectConfigFile(this._projCfg);
                        }
                        // Unsets
                        if (unsets.length > 0) {
                            let modified = this._projCfg.packages[packageName];
                            if (modified) {
                                for (let index in unsets) {
                                    let path = unsets[index];
                                    // TODO: Strip out any property assignments that dont conform to PackageExport
                                    //       (consider throwing error instead of continuing)
                                    _.unset(modified, path);
                                }
                                this._projCfg.packages[packageName] = modified;
                                yield this.projCfgRepo.updateProjectConfigFile(this._projCfg);
                            }
                        }
                        this._ui.userMessage(`COMPLETE!  Patched package [${packageName}]`);
                    }
                }
                else {
                    // Goddamn nullable dictionary
                    this._ui.userMessage(`The package export named [${packageName}] is null.`);
                    this._ui.userMessage(`No work was done.`);
                }
            }
            else {
                this._ui.userMessage(`No package named [${packageName}] is configured as an export in this project.`);
                this._ui.userMessage(`No work was done.`);
            }
        });
    }
}
exports.default = PkgPatch;
PkgPatch.description = 'Applies the given values to an existing package export configuration.  Both registryUrl and environment act as filters to limit the operation.  That is, an upsert of registry targets is performed for the given package, based on the given registryUrl and/or environment.';
PkgPatch.flags = {
    help: core_1.Flags.help({ char: 'h' }),
    set: core_1.Flags.string({ char: 's', required: false, description: 'Space-delimited list of key-value pairs tp apply with json-path on the left and the desired value on the right.  (eg:  --set .targets.default.registryUrl=mystuff.azurecr.io)' }),
    unset: core_1.Flags.string({ char: 'u', required: false, description: 'Space-delimited list of properties to clear, in json-path format.  (eg:  --unset .targets.default)' }),
};
PkgPatch.args = {
    packageName: core_1.Args.string({ required: true, description: 'package name including, scope prefix. eg:  "your-company/your-package"' })
};
//# sourceMappingURL=patch.js.map