"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

function _path() {
  const data = _interopRequireDefault(require("path"));

  _path = function () {
    return data;
  };

  return data;
}

function _stringFormat() {
  const data = _interopRequireDefault(require("string-format"));

  _stringFormat = function () {
    return data;
  };

  return data;
}

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

  _utils = function () {
    return data;
  };

  return data;
}

function _constants() {
  const data = require("../../constants");

  _constants = function () {
    return data;
  };

  return data;
}

class ConfigDir {
  constructor(dirPath) {
    this.dirPath = dirPath;
  }

  get linuxDirPath() {
    return (0, _utils().pathNormalizeToLinux)(this.dirPath);
  }

  get isUnderComponentDir() {
    return this.dirPath.startsWith(`{${_constants().COMPONENT_DIR}}`);
  }

  get hasEnvType() {
    return this.dirPath.includes('{ENV_TYPE}');
  }

  get isWorkspaceRoot() {
    const linDirPath = this.linuxDirPath;
    return linDirPath === '.' || linDirPath === './';
  }

  clone() {
    return new ConfigDir(this.dirPath);
  }

  replaceByComponentDirDSL(componentDir) {
    if (this.linuxDirPath.startsWith(componentDir)) {
      const replaced = this.linuxDirPath.replace(componentDir, `{${_constants().COMPONENT_DIR}}`);
      this.dirPath = _path().default.normalize(replaced);
    }
  }

  getCleaned({
    cleanComponentDir,
    cleanEnvType
  }) {
    const componentDir = cleanComponentDir ? '' : `{${_constants().COMPONENT_DIR}}`;
    const envType = cleanEnvType ? '' : '{ENV_TYPE}';
    const cleaned = (0, _stringFormat().default)(this.dirPath, {
      [`{${_constants().COMPONENT_DIR}}`]: componentDir,
      ENV_TYPE: envType
    });
    return new ConfigDir(cleaned);
  }

  getEnvTypeCleaned() {
    return this.getCleaned({
      cleanComponentDir: false,
      cleanEnvType: true
    });
  }

  getComponentDirCleaned() {
    return this.getCleaned({
      cleanComponentDir: true,
      cleanEnvType: false
    });
  }

  getResolved({
    componentDir,
    envType
  }) {
    const resolvedComponentDir = componentDir || `{${_constants().COMPONENT_DIR}}`;
    const resolvedEnvType = envType || '{ENV_TYPE}';
    const resolved = (0, _stringFormat().default)(this.dirPath, {
      [_constants().COMPONENT_DIR]: resolvedComponentDir,
      ENV_TYPE: resolvedEnvType
    });
    return new ConfigDir(resolved);
  }

}

exports.default = ConfigDir;