'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _path;

function _load_path() {
  return _path = _interopRequireDefault(require('path'));
}

var _ramda;

function _load_ramda() {
  return _ramda = _interopRequireDefault(require('ramda'));
}

var _constants;

function _load_constants() {
  return _constants = require('../constants');
}

var _logger;

function _load_logger() {
  return _logger = _interopRequireDefault(require('../logger/logger'));
}

var _gitHook;

function _load_gitHook() {
  return _gitHook = _interopRequireDefault(require('./git-hook'));
}

var _bitImportGitHook;

function _load_bitImportGitHook() {
  return _bitImportGitHook = _interopRequireDefault(require('./fixtures/bit-import-git-hook'));
}

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var HOOKS_DIR_NAME = 'hooks';

/* 
  * Setting up block level variable to store class state
  * set's to null by default.
*/
var 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
 */

var GitHooksManager = function () {
  function GitHooksManager(basePath) {
    _classCallCheck(this, GitHooksManager);

    this.hooks = new Map();

    this.basePath = basePath;
    if (!instance) {
      instance = this;
    }

    return instance;
  } // path to the .git dir


  _createClass(GitHooksManager, [{
    key: 'writeAllHooks',
    value: function writeAllHooks() {
      var alreadyExist = [];
      var added = [];
      this.hooks.forEach(function (hook, hookName) {
        var 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
     *
     */

  }, {
    key: 'hooksDirPath',
    get: function get() {
      return (_path || _load_path()).default.join(this.basePath, HOOKS_DIR_NAME);
    }

    /**
     * Initialize the default hooks
     */

  }], [{
    key: 'init',
    value: function init(basePath) {
      var self = new GitHooksManager(basePath);
      (_constants || _load_constants()).GIT_HOOKS_NAMES.forEach(function (hookName) {
        var hook = new (_gitHook || _load_gitHook()).default(self.hooksDirPath, hookName, (_bitImportGitHook || _load_bitImportGitHook()).default);
        self.hooks.set(hookName, hook);
      });
      return self;
    }
  }, {
    key: 'getInstance',
    value: function getInstance() {
      return instance;
    }
  }]);

  return GitHooksManager;
}();

exports.default = GitHooksManager;