package org.basex.query.func.array;

import org.basex.query.*;
import org.basex.query.expr.*;
import org.basex.query.func.fn.*;
import org.basex.query.iter.*;
import org.basex.query.value.*;
import org.basex.query.value.array.*;
import org.basex.query.value.item.*;

/**
 * Function implementation.
 *
 * @author BaseX Team 2005-18, BSD License
 * @author Christian Gruen
 */
public final class ArrayFoldLeft extends ArrayFn {
  @Override
  public Iter iter(final QueryContext qc) throws QueryException {
    return value(qc).iter();
  }

  @Override
  public Value value(final QueryContext qc) throws QueryException {
    final Array array = toArray(exprs[0], qc);
    Value result = exprs[1].value(qc);
    final FItem func = checkArity(exprs[2], 2, qc);
    for(final Value value : array.members()) result = func.invokeValue(qc, info, result, value);
    return result;
  }

  @Override
  protected Expr opt(final CompileContext cc) throws QueryException {
    FnFoldLeft.seqType(this, cc, true, true);
    return this;
  }
}
