var b = require('ast-types').builders;
module.exports = function(scope) {
  return b.functionExpression(
  null,
  [
    b.identifier('iterator'),
    b.identifier('index'),
    b.identifier('begin'),
    b.identifier('len')
  ],
  b.blockStatement([
    b.ifStatement(
      b.binaryExpression(
        '>',
        b.identifier('index'),
        b.identifier('begin')
      ),
      b.blockStatement([
        b.throwStatement(
          b.newExpression(
            b.identifier('RangeError'),
            []
          )
        )
      ]),
      null
    ),
    b.ifStatement(
      b.binaryExpression(
        '===',
        b.unaryExpression(
          'typeof',
          b.identifier('len'),
          true
        ),
        b.literal('undefined')
      ),
      b.blockStatement([
        b.expressionStatement(
          b.assignmentExpression(
            '=',
            b.identifier('len'),
            b.identifier('Infinity')
          )
        )
      ]),
      null
    ),
    b.variableDeclaration(
      'var',
      [
        b.variableDeclarator(
          b.identifier('range'),
          b.arrayExpression([])
        ),
        b.variableDeclarator(
          b.identifier('end'),
          b.binaryExpression(
            '+',
            b.identifier('begin'),
            b.identifier('len')
          )
        )
      ]
    ),
    b.whileStatement(
      b.binaryExpression(
        '<',
        b.identifier('index'),
        b.identifier('end')
      ),
      b.blockStatement([
        b.variableDeclaration(
          'var',
          [
            b.variableDeclarator(
              b.identifier('next'),
              b.callExpression(
                b.memberExpression(
                  b.identifier('iterator'),
                  b.identifier('next'),
                  false
                ),
                []
              )
            )
          ]
        ),
        b.ifStatement(
          b.memberExpression(
            b.identifier('next'),
            b.identifier('done'),
            false
          ),
          b.blockStatement([b.breakStatement()]),
          null
        ),
        b.ifStatement(
          b.binaryExpression(
            '>=',
            b.identifier('index'),
            b.identifier('begin')
          ),
          b.blockStatement([
            b.expressionStatement(
              b.callExpression(
                b.memberExpression(
                  b.identifier('range'),
                  b.identifier('push'),
                  false
                ),
                [
                  b.memberExpression(
                    b.identifier('next'),
                    b.identifier('value'),
                    false
                  )
                ]
              )
            )
          ]),
          null
        ),
        b.expressionStatement(
          b.updateExpression(
            '++',
            b.identifier('index'),
            false
          )
        )
      ])
    ),
    b.returnStatement(
      b.objectExpression([
        b.property(
          'init',
          b.identifier('range'),
          b.identifier('range')
        ),
        b.property(
          'init',
          b.identifier('index'),
          b.identifier('index')
        )
      ])
    )
  ]),
  false,
  false
)};
