/*!

* Chai - overwriteProperty utility
* Copyright(c) 2012-2014 Jake Luer <jake@alogicalparadox.com>
* MIT Licensed
*/

var chai = require('../../chai'); var flag = require('./flag'); var isProxyEnabled = require('./isProxyEnabled'); var transferFlags = require('./transferFlags');

/**

* ### .overwriteProperty(ctx, name, fn)
*
* Overwrites an already existing property getter and provides
* access to previous value. Must return function to use as getter.
*
*     utils.overwriteProperty(chai.Assertion.prototype, 'ok', function (_super) {
*       return function () {
*         var obj = utils.flag(this, 'object');
*         if (obj instanceof Foo) {
*           new chai.Assertion(obj.name).to.equal('bar');
*         } else {
*           _super.call(this);
*         }
*       }
*     });
*
*
* Can also be accessed directly from `chai.Assertion`.
*
*     chai.Assertion.overwriteProperty('foo', fn);
*
* Then can be used as any other assertion.
*
*     expect(myFoo).to.be.ok;
*
* @param {Object} ctx object whose property is to be overwritten
* @param {String} name of property to overwrite
* @param {Function} getter function that returns a getter function to be used for name
* @namespace Utils
* @name overwriteProperty
* @api public
*/

module.exports = function overwriteProperty(ctx, name, getter) {

var _get = Object.getOwnPropertyDescriptor(ctx, name)
  , _super = function () {};

if (_get && 'function' === typeof _get.get)
  _super = _get.get

Object.defineProperty(ctx, name,
  { get: function overwritingPropertyGetter() {
      // Setting the `ssfi` flag to `overwritingPropertyGetter` causes this
      // function to be the starting point for removing implementation frames
      // from the stack trace of a failed assertion.
      //
      // However, we only want to use this function as the starting point if
      // the `lockSsfi` flag isn't set and proxy protection is disabled.
      //
      // If the `lockSsfi` flag is set, then either this assertion has been
      // overwritten by another assertion, or this assertion is being invoked
      // from inside of another assertion. In the first case, the `ssfi` flag
      // has already been set by the overwriting assertion. In the second
      // case, the `ssfi` flag has already been set by the outer assertion.
      //
      // If proxy protection is enabled, then the `ssfi` flag has already been
      // set by the proxy getter.
      if (!isProxyEnabled() && !flag(this, 'lockSsfi')) {
        flag(this, 'ssfi', overwritingPropertyGetter);
      }

      // Setting the `lockSsfi` flag to `true` prevents the overwritten
      // assertion from changing the `ssfi` flag. By this point, the `ssfi`
      // flag is already set to the correct starting point for this assertion.
      var origLockSsfi = flag(this, 'lockSsfi');
      flag(this, 'lockSsfi', true);
      var result = getter(_super).call(this);
      flag(this, 'lockSsfi', origLockSsfi);

      if (result !== undefined) {
        return result;
      }

      var newAssertion = new chai.Assertion();
      transferFlags(this, newAssertion);
      return newAssertion;
    }
  , configurable: true
});

};