var b = require('ast-types').builders;
module.exports = function(scope) {
  var getArrayIterator = require('..').getArrayIterator;

  return b.functionExpression(
  null,
  [b.identifier('iterable')],
  b.blockStatement([
    b.variableDeclaration(
      'var',
      [
        b.variableDeclarator(
          b.identifier('sym'),
          b.logicalExpression(
            '||',
            b.logicalExpression(
              '&&',
              b.binaryExpression(
                '===',
                b.unaryExpression(
                  'typeof',
                  b.identifier('Symbol'),
                  true
                ),
                b.literal('function')
              ),
              b.memberExpression(
                b.identifier('Symbol'),
                b.identifier('iterator'),
                false
              )
            ),
            b.literal('@@iterator')
          )
        )
      ]
    ),
    b.ifStatement(
      b.binaryExpression(
        '===',
        b.unaryExpression(
          'typeof',
          b.memberExpression(
            b.identifier('iterable'),
            b.identifier('sym'),
            true
          ),
          true
        ),
        b.literal('function')
      ),
      b.blockStatement([
        b.returnStatement(
          b.callExpression(
            b.memberExpression(
              b.identifier('iterable'),
              b.identifier('sym'),
              true
            ),
            []
          )
        )
      ]),
      b.ifStatement(
        b.logicalExpression(
          '||',
          b.binaryExpression(
            '===',
            b.unaryExpression(
              'typeof',
              b.identifier('iterable'),
              true
            ),
            b.literal('object')
          ),
          b.binaryExpression(
            '===',
            b.unaryExpression(
              'typeof',
              b.identifier('iterable'),
              true
            ),
            b.literal('function')
          )
        ),
        b.blockStatement([
          b.returnStatement(
            b.callExpression(
              getArrayIterator(scope),
              [b.identifier('iterable')]
            )
          )
        ]),
        b.blockStatement([
          b.throwStatement(
            b.newExpression(
              b.identifier('TypeError'),
              []
            )
          )
        ])
      )
    )
  ]),
  false,
  false
)};
