const { DecimalPlaceIsNonZero, GetDecimalInPlace } = require("./common");

module.exports = {
    /**
     * Position Mode
     *
     * When the int computer is in Position mode, the value at the pointer
     * is interpreted as another address in memory.
     */
    POSITION_MODE: 0,
    /**
     * Immediate Mode
     *
     * When the int computer is in Immediate Mode, the value at the pointer
     * is interpreted as its literal value.
     */
    IMMEDIATE_MODE: 1,
    /**
     * Relative Mode
     *
     * Unknown.
     */
    RELATIVE_MODE: 2,
    /**
     * Parse an opcode to get the Parameter Mode for an operation's parameter
     *
     * Note: `parameterIndex` is a 1-based index, not 0-based.
     *
     * @example
     * // POSITION_MODE
     * const firstParamMode = ComputerParameterMode.ParseParameterMode(1002, 1);
     * // IMMEDIATE_MODE
     * const secondParamMode = ComputerParameterMode.ParseParameterMode(1002, 2);
     *
     * @param {number} rawOpcode The opcode in memory
     * @param {number} parameterIndex The index of the parameter in the function arguments
     * @returns {number} Either POSITION_MODE or IMMEDIATE_MODE
     */
    ParseParameterMode: function (rawOpcode, parameterIndex) {
        const paramModeFromOpcode = GetDecimalInPlace(rawOpcode, parameterIndex + 1);
        switch (paramModeFromOpcode) {
            case 0: return this.POSITION_MODE;
            case 1: return this.IMMEDIATE_MODE;
            case 2: return this.RELATIVE_MODE;
            default: return this.POSITION_MODE;
        }
    },
};