"use strict";

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

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

function _bluebird() {
  const data = require("bluebird");

  _bluebird = function () {
    return data;
  };

  return data;
}

function _defineProperty2() {
  const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

  _defineProperty2 = function () {
    return data;
  };

  return data;
}

function _ramda() {
  const data = _interopRequireDefault(require("ramda"));

  _ramda = function () {
    return data;
  };

  return data;
}

function _fsExtra() {
  const data = _interopRequireDefault(require("fs-extra"));

  _fsExtra = function () {
    return data;
  };

  return data;
}

function _diagnosis() {
  const data = _interopRequireDefault(require("../diagnosis"));

  _diagnosis = function () {
    return data;
  };

  return data;
}

function _consumer() {
  const data = require("../../consumer");

  _consumer = function () {
    return data;
  };

  return data;
}

function _workspaceConfig() {
  const data = _interopRequireDefault(require("../../consumer/config/workspace-config"));

  _workspaceConfig = function () {
    return data;
  };

  return data;
}

function _abstractConfig() {
  const data = _interopRequireDefault(require("../../consumer/config/abstract-config"));

  _abstractConfig = function () {
    return data;
  };

  return data;
}

class ValidateWorkspaceBitJsonSyntax extends _diagnosis().default {
  constructor(...args) {
    super(...args);
    (0, _defineProperty2().default)(this, "name", "validate workspace's bit config");
    (0, _defineProperty2().default)(this, "description", 'validate workspace configuration object');
    (0, _defineProperty2().default)(this, "category", 'configuration');
  }

  _formatSymptoms(bareResult) {
    const bitJsonPath = _ramda().default.path(['data', 'bitJsonPath'], bareResult);

    return `invalid bit.json: ${bitJsonPath} is not a valid JSON file.`;
  }

  _formatManualTreat() {
    return 'manually fix the bit.json or consider running bit init --reset to recreate the file';
  } // TODO: support configuraiton from package.json


  _runExamine() {
    return (0, _bluebird().coroutine)(function* () {
      const consumer = yield (0, _consumer().loadConsumer)();
      const consumerPath = consumer.getPath();

      const bitJsonPath = _abstractConfig().default.composeBitJsonPath(consumerPath);

      const exist = yield _fsExtra().default.exists(bitJsonPath);

      if (!exist) {
        return {
          valid: true
        };
      }

      try {
        _workspaceConfig().default.loadBitJson(bitJsonPath);

        return {
          valid: true
        };
      } catch (e) {
        return {
          valid: false,
          data: {
            bitJsonPath
          }
        };
      }
    })();
  }

}

exports.default = ValidateWorkspaceBitJsonSyntax;