"use strict";

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

var _path = _interopRequireDefault(require("path"));

var _fs = _interopRequireDefault(require("fs"));

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

const cleanArchFileName = 'clean-architecture-ts-api';
const hexagonalArchFileName = 'hexagonal-ts-api';
let typeTemplate;
exports.typeTemplate = typeTemplate;

(function (typeTemplate) {
  typeTemplate[typeTemplate["CLEAN_ARCH"] = 0] = "CLEAN_ARCH";
  typeTemplate[typeTemplate["HEXA_ARCH"] = 1] = "HEXA_ARCH";
})(typeTemplate || (exports.typeTemplate = typeTemplate = {}));

let createTemplateError;
exports.createTemplateError = createTemplateError;

(function (createTemplateError) {
  createTemplateError[createTemplateError["PROJECT_ALREADY_EXIST"] = 0] = "PROJECT_ALREADY_EXIST";
  createTemplateError[createTemplateError["UNKNOWN_ERROR"] = 1] = "UNKNOWN_ERROR";
})(createTemplateError || (exports.createTemplateError = createTemplateError = {}));

const createTemplate = async (data, copyFile) => {
  const {
    name,
    template
  } = data;
  const {
    PROJECT_ALREADY_EXIST,
    UNKNOWN_ERROR
  } = createTemplateError;
  const templateName = template === typeTemplate.CLEAN_ARCH ? cleanArchFileName : hexagonalArchFileName;
  const {
    dest,
    source
  } = findPath(name, templateName);

  if (alreadyProjectCreated(dest)) {
    return PROJECT_ALREADY_EXIST;
  }

  const created = await copyFile.copy(source, dest);

  if (!created) {
    return UNKNOWN_ERROR;
  }

  return dest;
};

exports.createTemplate = createTemplate;

function alreadyProjectCreated(dest) {
  return _fs.default.existsSync(dest);
}

function findPath(name, template) {
  const source = _path.default.join(__dirname, 'templates' + '/' + template);

  const dest = name !== undefined && name !== '' && name !== null ? name : template;
  return {
    dest,
    source
  };
}