'use strict';
var $ = require('../internals/export');
var DESCRIPTORS = require('../internals/descriptors');
var anObject = require('../internals/an-object');
var toPropertyKey = require('../internals/to-property-key');
var definePropertyModule = require('../internals/object-define-property');
var isCallable = require('../internals/is-callable');
var fails = require('../internals/fails');

var $TypeError = TypeError;

// MS Edge has broken Reflect.defineProperty - throwing instead of returning false
var ERROR_INSTEAD_OF_FALSE = fails(function () {
  // eslint-disable-next-line es/no-reflect -- required for testing
  Reflect.defineProperty(definePropertyModule.f({}, 1, { value: 1 }), 1, { value: 2 });
});

// `Reflect.defineProperty` method
// https://tc39.es/ecma262/#sec-reflect.defineproperty
$({ target: 'Reflect', stat: true, forced: ERROR_INSTEAD_OF_FALSE, sham: !DESCRIPTORS }, {
  defineProperty: function defineProperty(target, propertyKey, attributes) {
    anObject(target);
    var key = toPropertyKey(propertyKey);
    var get, set;
    anObject(attributes);
    // propagate `ToPropertyDescriptor` errors instead of catching them
    if (('get' in attributes || 'set' in attributes) && (
      ('get' in attributes && !isCallable(get = attributes.get) && get !== undefined) ||
      ('set' in attributes && !isCallable(set = attributes.set) && set !== undefined) ||
      ('value' in attributes || 'writable' in attributes)
    )) throw new $TypeError('Invalid property descriptor');
    try {
      definePropertyModule.f(target, key, attributes);
      return true;
    } catch (error) {
      return false;
    }
  }
});
