import * as Commands from './../../ast/commands';
import { Types } from './../../typeSystem/types';
import { toInt } from "./../../typeSystem/parsers";
import { ArrayType } from '../../typeSystem/array_type';
import { Modes } from '../modes';
import { StoreValue } from '../store/value/store_value';

/**
 * num_elements
 * matrix_lines
 * matrix_columns
 */

export function createNumElementsFun () {
  const numElementsFun = async (sto, _) => {
    const vector  = sto.applyStore("vector");
    const temp = new StoreValue(Types.INTEGER, toInt(vector.lines));
    sto.mode = Modes.RETURN;
    sto.insertStore("$", temp);
    return sto;
  }

  const block = new Commands.CommandBlock([],  [new Commands.SysCall(numElementsFun)]);
  const func = new Commands.Function('$numElements', Types.INTEGER,
    [new Commands.FormalParameter(new ArrayType(Types.ALL, 1), 'vector', false)],
    block);
  return func;
 }

export function createMatrixLinesFun () {
  const matrixLinesFun = async (sto, _) => {
    const matrix  = sto.applyStore("matrix");
    const temp = new StoreValue(Types.INTEGER, toInt(matrix.lines));
    sto.mode = Modes.RETURN;
    sto.insertStore("$", temp);
    return sto;
  }

  const block = new Commands.CommandBlock([],  [new Commands.SysCall(matrixLinesFun)]);
  const func = new Commands.Function('$matrixLines', Types.INTEGER,
    [new Commands.FormalParameter(new ArrayType(Types.ALL, 2), 'matrix', false)],
    block);
  return func;
 }

export function createMatrixColumnsFun () {
  const matrixColumnsFun = async (sto, _) => {
    const matrix  = sto.applyStore("matrix");
    const temp = new StoreValue(Types.INTEGER, toInt(matrix.columns));
    sto.mode = Modes.RETURN;
    sto.insertStore("$", temp);
    return sto;
  }

  const block = new Commands.CommandBlock([],  [new Commands.SysCall(matrixColumnsFun)]);
  const func = new Commands.Function('$matrixColumns', Types.INTEGER,
    [new Commands.FormalParameter(new ArrayType(Types.ALL, 2), 'matrix', false)],
    block);
  return func;
 }
 