"use strict";

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

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

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

  _constants = function () {
    return data;
  };

  return data;
}

function _generalError() {
  const data = _interopRequireDefault(require("../../error/general-error"));

  _generalError = function () {
    return data;
  };

  return data;
}

function _configDir() {
  const data = _interopRequireDefault(require("../bit-map/config-dir"));

  _configDir = function () {
    return data;
  };

  return data;
}

function _logger() {
  const data = _interopRequireDefault(require("../../logger/logger"));

  _logger = function () {
    return data;
  };

  return data;
}

class BitStructure {
  constructor(componentsDefaultDirectory, dependenciesDirectory, ejectedEnvsDirectory) {
    this._componentsDefaultDirectoryUnProcessed = componentsDefaultDirectory || _constants().DEFAULT_COMPONENTS_DIR_PATH;
    this.dependenciesDirectory = dependenciesDirectory || _constants().DEFAULT_DEPENDENCIES_DIR_PATH;
    this.ejectedEnvsDirectory = ejectedEnvsDirectory || _constants().DEFAULT_EJECTED_ENVS_DIR_PATH;
  }

  get dependenciesDirStructure() {
    return this.dependenciesDirectory;
  }

  get ejectedEnvsDirStructure() {
    return new (_configDir().default)(this.ejectedEnvsDirectory);
  }

  get componentsDefaultDirectory() {
    if (!this._componentsDefaultDirectory) {
      const dirStructure = this._componentsDefaultDirectoryUnProcessed;
      const dirStructureParsed = [];
      dirStructure.split('/').forEach(dir => {
        if (dir.startsWith('{') && dir.endsWith('}')) {
          // this is a dynamic parameter
          const dirStripped = dir.replace(/[{}]/g, '');

          const componentPart = this._getComponentStructurePart(dirStructure, dirStripped);

          if (componentPart) dirStructureParsed.push(`{${componentPart}}`);
        } else {
          dirStructureParsed.push(dir);
        }
      });
      this._componentsDefaultDirectory = dirStructureParsed.join('/');
    }

    return this._componentsDefaultDirectory;
  }

  _getComponentStructurePart(componentStructure, componentPart) {
    switch (componentPart) {
      case 'name':
      case 'scope':
      case 'version':
        return componentPart;

      case 'namespace':
        _logger().default.warn('your bit.json has an obsolete "namespace" set in componentsDefaultDirectory property, it has been ignored');

        return '';
      // this the dynamic namespace feature, the namespace doesn't exist, it's part of the name

      default:
        throw new (_generalError().default)(`the "${componentPart}" part of the component structure "${componentStructure}" is invalid, it must be one of the following: "name", "scope" or "version" `);
    }
  }

}

exports.default = BitStructure;