"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getVersionName = getVersionName;
exports.setVersionName = setVersionName;
exports.getVersionCode = getVersionCode;
exports.setVersionCode = setVersionCode;
exports.withVersion = void 0;

function _androidPlugins() {
  const data = require("../plugins/android-plugins");

  _androidPlugins = function () {
    return data;
  };

  return data;
}

function _warnings() {
  const data = require("../utils/warnings");

  _warnings = function () {
    return data;
  };

  return data;
}

const withVersion = config => {
  return (0, _androidPlugins().withAppBuildGradle)(config, config => {
    if (config.modResults.language === 'groovy') {
      config.modResults.contents = setVersionCode(config, config.modResults.contents);
      config.modResults.contents = setVersionName(config, config.modResults.contents);
    } else {
      (0, _warnings().addWarningAndroid)('android.versionCode', `Cannot automatically configure app build.gradle if it's not groovy`);
    }

    return config;
  });
};

exports.withVersion = withVersion;

function getVersionName(config) {
  var _config$version;

  return (_config$version = config.version) !== null && _config$version !== void 0 ? _config$version : null;
}

function setVersionName(config, buildGradle) {
  const versionName = getVersionName(config);

  if (versionName === null) {
    return buildGradle;
  }

  const pattern = new RegExp(`versionName ".*"`);
  return buildGradle.replace(pattern, `versionName "${versionName}"`);
}

function getVersionCode(config) {
  var _config$android$versi, _config$android;

  return (_config$android$versi = (_config$android = config.android) === null || _config$android === void 0 ? void 0 : _config$android.versionCode) !== null && _config$android$versi !== void 0 ? _config$android$versi : 1;
}

function setVersionCode(config, buildGradle) {
  const versionCode = getVersionCode(config);

  if (versionCode === null) {
    return buildGradle;
  }

  const pattern = new RegExp(`versionCode.*`);
  return buildGradle.replace(pattern, `versionCode ${versionCode}`);
}
//# sourceMappingURL=Version.js.map