/*!
 * base-config <https://github.com/jonschlinkert/base-config>
 *
 * Copyright (c) 2015, Jon Schlinkert.
 * Licensed under the MIT License.
 */

'use strict';

var map = require('map-config');

module.exports = function(method) {
  method || (method = 'config');

  return function plugin(app) {
    var mapper = map(app);
    var fn = store(app.store);
    console.log(fn.process)
    mapper.alias('show', 'get')
      // .map('store', store(app.store))
      .map('store', fn)
      .map('option')
      .map('data')
      .map('set')
      .map('del')
      .map('get')
      .map('has');

    app.mixin(method, proxy(mapper));
    return app;
  };

  function store(app) {
    var config = map(app)
      .alias('show', 'get')
      .map('set')
      .map('del')
      .map('has')
      .map('get');

    return function(argv) {
      config.process(argv);
    };
  }
};

function proxy(mapper) {
  function config(key, value) {
    if (typeof key === 'string') {
      mapper.map.apply(mapper, arguments);

    } else if (key && typeof key === 'object') {
      for (var prop in key) {
        config(prop, key[prop]);
      }
    }
    return mapper;
  }
  config.__proto__ = mapper;
  return config;
}

/*!
 * base-config <https://github.com/jonschlinkert/base-config>
 *
 * Copyright (c) 2015, Jon Schlinkert.
 * Licensed under the MIT License.
 */

'use strict';

var mapper = require('map-config');
var isObject = require('isobject');

module.exports = function() {
  return function(app) {
    var config = mapper(app)
      .map('data')
      .map('store', store(app.store))
      .map('option')
      .map('enable')
      .map('enabled')
      .map('disable')
      .map('disabled')
      .map('set')
      .map('del')
      .map('get')
      .map('has');

    app.define('config', proxy(config));
  };

  function store(app) {
    if (!app) return {};
    var config = mapper(app)
      .map('set')
      .map('del')
      .map('has')
      .map('get');

    app.define('config', config);
    return function(argv) {
      config.process(argv);
    };
  }
};

/**
 * Proxy to support config as a function and object
 * with methods, allowing the user to do either of
 * the following:
 *
 * ```js
 * base.config({
 *   foo: 'bar'
 * });
 *
 * // or
 * base.config.map('foo', 'bar');
 * ```
 */

function proxy(config) {
  function fn(key, val) {
    if (typeof val === 'string') {
      config.alias.apply(config, arguments);
      return config;
    }

    if (typeof key === 'string') {
      config.map.apply(config, arguments);
      return config;
    }

    if (!isObject(key)) {
      throw new TypeError('expected key to be a string or object');
    }

    for (var prop in key) {
      fn(prop, key[prop]);
    }
    return config;
  }
  fn.__proto__ = config;
  return fn;
}

clean up proxy