'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.defineTestCaseHook = defineTestCaseHook;
exports.defineTestRunHook = defineTestRunHook;
exports.defineStep = defineStep;
exports.defineParameterType = defineParameterType;

var _util = require('util');

var _lodash = require('lodash');

var _lodash2 = _interopRequireDefault(_lodash);

var _helpers = require('../formatter/helpers');

var _cucumberExpressions = require('cucumber-expressions');

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

var _stacktraceJs = require('stacktrace-js');

var _stacktraceJs2 = _interopRequireDefault(_stacktraceJs);

var _step_definition = require('../models/step_definition');

var _step_definition2 = _interopRequireDefault(_step_definition);

var _test_case_hook_definition = require('../models/test_case_hook_definition');

var _test_case_hook_definition2 = _interopRequireDefault(_test_case_hook_definition);

var _test_run_hook_definition = require('../models/test_run_hook_definition');

var _test_run_hook_definition2 = _interopRequireDefault(_test_run_hook_definition);

var _validate_arguments = require('./validate_arguments');

var _validate_arguments2 = _interopRequireDefault(_validate_arguments);

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

function defineTestCaseHook(builder, collectionName) {
  return function (options, code) {
    if (typeof options === 'string') {
      options = { tags: options };
    } else if (typeof options === 'function') {
      code = options;
      options = {};
    }

    var _getDefinitionLineAnd = getDefinitionLineAndUri(builder.cwd),
        line = _getDefinitionLineAnd.line,
        uri = _getDefinitionLineAnd.uri;

    (0, _validate_arguments2.default)({
      args: { code: code, options: options },
      fnName: 'defineTestCaseHook',
      location: (0, _helpers.formatLocation)({ line: line, uri: uri })
    });
    var hookDefinition = new _test_case_hook_definition2.default({
      code: code,
      line: line,
      options: options,
      uri: uri
    });
    builder.options[collectionName].push(hookDefinition);
  };
}

function defineTestRunHook(builder, collectionName) {
  return function (options, code) {
    if (typeof options === 'string') {
      options = { tags: options };
    } else if (typeof options === 'function') {
      code = options;
      options = {};
    }

    var _getDefinitionLineAnd2 = getDefinitionLineAndUri(builder.cwd),
        line = _getDefinitionLineAnd2.line,
        uri = _getDefinitionLineAnd2.uri;

    (0, _validate_arguments2.default)({
      args: { code: code, options: options },
      fnName: 'defineTestRunHook',
      location: (0, _helpers.formatLocation)({ line: line, uri: uri })
    });
    var hookDefinition = new _test_run_hook_definition2.default({
      code: code,
      line: line,
      options: options,
      uri: uri
    });
    builder.options[collectionName].push(hookDefinition);
  };
}

function defineStep(builder) {
  return function (pattern, options, code) {
    if (typeof options === 'function') {
      code = options;
      options = {};
    }

    var _getDefinitionLineAnd3 = getDefinitionLineAndUri(builder.cwd),
        line = _getDefinitionLineAnd3.line,
        uri = _getDefinitionLineAnd3.uri;

    (0, _validate_arguments2.default)({
      args: { code: code, pattern: pattern, options: options },
      fnName: 'defineStep',
      location: (0, _helpers.formatLocation)({ line: line, uri: uri })
    });
    var stepDefinition = new _step_definition2.default({
      code: code,
      line: line,
      options: options,
      pattern: pattern,
      uri: uri
    });
    builder.options.stepDefinitions.push(stepDefinition);
  };
}

var projectPath = _path2.default.join(__dirname, '..', '..');
var projectSrcPath = _path2.default.join(projectPath, 'src');
var projectLibPath = _path2.default.join(projectPath, 'lib');

function getDefinitionLineAndUri(cwd) {
  var line = 'unknown';
  var uri = 'unknown';
  var stackframes = _stacktraceJs2.default.getSync();
  var stackframe = _lodash2.default.find(stackframes, function (frame) {
    var filename = frame.getFileName();
    return !_lodash2.default.includes(filename, projectSrcPath) && !_lodash2.default.includes(filename, projectLibPath);
  });
  if (stackframe) {
    line = stackframe.getLineNumber();
    uri = stackframe.getFileName();
    if (uri) {
      uri = _path2.default.relative(cwd, uri);
    }
  }
  return { line: line, uri: uri };
}

function defineParameterType(builder) {
  return function (_ref) {
    var name = _ref.name,
        typeName = _ref.typeName,
        regexp = _ref.regexp,
        transformer = _ref.transformer,
        useForSnippets = _ref.useForSnippets,
        preferForRegexpMatch = _ref.preferForRegexpMatch;

    var getTypeName = (0, _util.deprecate)(function () {
      return typeName;
    }, 'Cucumber defineParameterType: Use name instead of typeName');
    var _name = name || getTypeName();
    if (typeof useForSnippets !== 'boolean') useForSnippets = true;
    if (typeof preferForRegexpMatch !== 'boolean') preferForRegexpMatch = false;
    var parameterType = new _cucumberExpressions.ParameterType(_name, regexp, null, transformer, useForSnippets, preferForRegexpMatch);
    builder.options.parameterTypeRegistry.defineParameterType(parameterType);
  };
}
//# sourceMappingURL=data:application/json;charset=utf-8;base64,[encoded data, 10632 characters]