const ParseProblem = require('../parsetypes/parse-problem');
const {SourceType} = require('../source-type');
const { UnresolvedType } = require('java-mti');

/**
 * @param {SourceType} source_type 
 * @param {*} probs 
 */
function checkImplements(source_type, probs) {
    if (source_type.implements_types.length === 0) {
        return;
    }
    const interfaces = source_type.implements_types.map(it => it.resolved);
    if (source_type.typeKind === 'interface') {
        probs.push(ParseProblem.Error(source_type.implements_types[0].tokens, `Interface types cannot declare an implements section`));
    }
    if (source_type.typeKind === 'class') {
        interfaces.forEach((intf, i) => {
            if (intf instanceof UnresolvedType) {
                return;
            }
            if (intf.typeKind !== 'interface') {
                probs.push(ParseProblem.Error(source_type.implements_types[i].tokens, `Class '${source_type.fullyDottedRawName}' cannot implement ${intf.typeKind} type: '${intf.fullyDottedRawName}'`));
            }
        })
    }
}

/**
 * @param {SourceType[]} source_types
 */
module.exports = function(source_types) {
    /** @type {ParseProblem[]} */
    const probs = [];
      
    source_types.forEach(type => checkImplements(type, probs));
      
    return probs;
}
