"use strict";

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

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

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

  _defineProperty2 = function () {
    return data;
  };

  return data;
}

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

  _path = function () {
    return data;
  };

  return data;
}

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

  _ramda = function () {
    return data;
  };

  return data;
}

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

  _constants = function () {
    return data;
  };

  return data;
}

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

  _logger = function () {
    return data;
  };

  return data;
}

function _gitHook() {
  const data = _interopRequireDefault(require("./git-hook"));

  _gitHook = function () {
    return data;
  };

  return data;
}

function _bitImportGitHook() {
  const data = _interopRequireDefault(require("./fixtures/bit-import-git-hook"));

  _bitImportGitHook = function () {
    return data;
  };

  return data;
}

const HOOKS_DIR_NAME = 'hooks';
/* 
  * Setting up block level variable to store class state
  * set's to null by default.
*/

let instance = null;
/**
 * A class which manage all the git hooks
 * This is a singleton class which expose getInstance method
 * This class used for add new git hooks
 */

class GitHooksManager {
  // path to the .git dir
  constructor(basePath) {
    (0, _defineProperty2().default)(this, "hooks", new Map());
    this.basePath = basePath;

    if (!instance) {
      instance = this;
    }

    return instance;
  }

  get hooksDirPath() {
    return _path().default.join(this.basePath, HOOKS_DIR_NAME);
  }
  /**
   * Initialize the default hooks
   */


  static init(basePath) {
    const self = new GitHooksManager(basePath);

    _constants().GIT_HOOKS_NAMES.forEach(hookName => {
      const hook = new (_gitHook().default)(self.hooksDirPath, hookName, _bitImportGitHook().default);
      self.hooks.set(hookName, hook);
    });

    return self;
  }

  writeAllHooks() {
    const alreadyExist = [];
    const added = [];
    this.hooks.forEach((hook, hookName) => {
      const result = hook.writeSync();

      if (result) {
        added.push(hookName);
      } else {
        alreadyExist.push(hookName);
      }
    });
    return {
      added,
      alreadyExist
    };
  }
  /**
   * Get the instance of the HooksManager
   * @return {GitHooksManager} instance of the GitHooksManager
   *
   */


  static getInstance() {
    return instance;
  }

}

exports.default = GitHooksManager;