"use strict";

exports.__esModule = true;
exports.default = _default;

var _core = require("@babel/core");

function _default(decorators, scope) {
  for (var _iterator = decorators, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
    var _ref;

    if (_isArray) {
      if (_i >= _iterator.length) break;
      _ref = _iterator[_i++];
    } else {
      _i = _iterator.next();
      if (_i.done) break;
      _ref = _i.value;
    }

    var _decorator = _ref;
    var expression = _decorator.expression;
    if (!_core.types.isMemberExpression(expression)) continue;
    var temp = scope.maybeGenerateMemoised(expression.object);
    var ref = void 0;
    var nodes = [];

    if (temp) {
      ref = temp;
      nodes.push(_core.types.assignmentExpression("=", temp, expression.object));
    } else {
      ref = expression.object;
    }

    nodes.push(_core.types.callExpression(_core.types.memberExpression(_core.types.memberExpression(ref, expression.property, expression.computed), _core.types.identifier("bind")), [ref]));

    if (nodes.length === 1) {
      _decorator.expression = nodes[0];
    } else {
      _decorator.expression = _core.types.sequenceExpression(nodes);
    }
  }

  return decorators;
}