"use strict";

var gulp = require('gulp'),
    replace = require('gulp-replace'),
    semver = require('semver');

var m = require('../common.js');
var argv = require('yargs').argv;

const INC_VERSION = {
    "release-patch": "patch",
    "release-minor": "minor",
    "release-major": "major",
    "release-beta": "prerelease",
    "publish-beta": "prerelease",
    "publish-tag": "prerelease",
    "publish-major": "major"
};

//bump versions on package
gulp.task('bump', () => {
    const bump = require('gulp-bump');
    var jeditor = require("gulp-json-editor");
    var newVer;
    // reget package
    var pkg = m.getPackageJson();

	  var incVersion = 'patch';
    var versionArg = argv._[0];

	  if (INC_VERSION[versionArg]) {
		  incVersion = INC_VERSION[versionArg];
    }

    // increment version
    if (versionArg === "publish-tag"){
        newVer = semver.inc(pkg.version, incVersion, argv.tag);
    }
    else {
        newVer = semver.inc(pkg.version, incVersion, 'beta');
    }

    const updateDependencies = json => {
        if (json.devDependencies) {
            if (json.devDependencies['@formbird/services']) {
                json.devDependencies['@formbird/services'] = `${newVer}`;
            }

            if (json.devDependencies['@formbird/types']) {
              json.devDependencies['@formbird/types'] = `${newVer}`;
            }

            if (json.devDependencies['@formbird/shared']) {
                json.devDependencies['@formbird/shared'] = `${newVer}`;
            }

            if (json.devDependencies['@formbird/styles']) {
                json.devDependencies['@formbird/styles'] = `${newVer}`;
            }

            if (json.devDependencies['@formbird/angular-shared']) {
                json.devDependencies['@formbird/angular-shared'] = `${newVer}`;
            }
        }

        return json;
    };

    const bumpPackage = (packageName) => {
        let src = `./projects/${packageName}/`;
        if (packageName === 'services') {
          src = './client/app/modules/formbird-services/';
        }
        return gulp.src([`${src}package.json`])
            .pipe(bump({
                version: newVer
            }))
            .pipe(jeditor(updateDependencies))
            .pipe(gulp.dest(src));
    };

    const bumpYarn = () => {
        return gulp.src(['./yarn.lock'])
          .pipe(replace(`@formbird/services@${pkg.version}`, `@formbird/services@${newVer}`))
          .pipe(replace(`@formbird/shared@${pkg.version}`, `@formbird/shared@${newVer}`))
          .pipe(replace(`@formbird/styles@${pkg.version}`, `@formbird/styles@${newVer}`))
          .pipe(replace(`@formbird/types@${pkg.version}`, `@formbird/types@${newVer}`))
          .pipe(replace(`@formbird/angular-shared@${pkg.version}`, `@formbird/angular-shared@${newVer}`))
          .pipe(gulp.dest('./'));
    };

    bumpPackage('shared');
    bumpPackage('types');
    bumpPackage('services');
    bumpPackage('styles');
    bumpPackage('angular-shared');
    bumpYarn();

    //updating new version
    return gulp.src(['./package.json'])
        .pipe(bump({
            version: newVer
        }))
        .pipe(jeditor(updateDependencies))
        .pipe(gulp.dest('./'));
});
