'use strict';

var path = require('path');
var jsVars = require('interpret').jsVariants;
var globby = require('globby');
var debug = require('debug');
var _ = debug('fly:find');
// promisify `fs` methods
var thenify = require('thenify-all');
var fs = thenify(require('fs'), {}, ['stat']);

/**
 * Find a valid Flyfile from a given path
 * @param {String}   filepath      The file or path to a Flyfile
 * @param {Function} bind          The function to bind, require, or process `filepath`
 * @return {String}   						 The path to the Flyfile
 */
module.exports = function * (filepath, bind) {
	bind = bind || function(_) {
		return _;
	};

	_('resolve path to flyfile %o', filepath);

	var froot = path.join(process.cwd(), filepath);
	var stats = yield fs.stat(filepath);

	if (stats.isDirectory()) {
		var glob = fileOptions();
		froot = yield globby(froot + path.sep + glob);
	}

	return froot;
};

/**
 * Format a Glob string for possible flyfile matching
 * @return {String}
 */
function fileOptions() {
	var extns = Object.keys(jsVars);
	var names = ['Flyfile', 'flyfile'];

	return [names, extns].map(function (obj) {
		return '{' + obj.join('|') + '}';
	}).join('.')
}

@@ -614,14 +614,15 @@
 tion
+ 
 (_) %7B%0A
-
 %09%09re
@@ -872,16 +872,79 @@
  glob);%0A
+%09%09console.log('inside utils.find', froot);%0A%09%09froot = froot%5B0%5D;%0A
 %09%7D%0A%0A%09ret
@@ -1217,16 +1217,16 @@
  + '%7D';%0A
-
 %09%7D).join
@@ -1230,11 +1230,12 @@
 oin('.')
+;
 %0A%7D%0A

fix linting