// 物品表
import { readFileAndParse, decodeArrayListStr, } from '../util'
import { FILENAME, } from '../../consts'
const _ = require('lodash');

export interface DicArtifactQualityPlan { 
    // 方案id
    planId: number;
    // 这级的属性
    attr: {id: number, attr: number}[];
}

type KeysEnum<T> = { [P in keyof Required<T>]: true };
const DicArtifactQualityPlanKeys: KeysEnum<DicArtifactQualityPlan> = {
    planId: true,
    attr: true,
}
export const dicArtifactQualityPlan = new Map<number, DicArtifactQualityPlan>();

export function loadArtifactQualityPlan() {
    dicArtifactQualityPlan.clear();
    
    let arr = readFileAndParse(FILENAME.DIC_ARTIFACT_QUALITY_PLAN);

    arr.forEach(o => {
        o.attr = parseAttr(o.attr);
        dicArtifactQualityPlan.set(o.planId, _.pick(o, Object.keys(DicArtifactQualityPlanKeys)));
    });

    arr = undefined;
}

function parseAttr(str: string) {
    let result = new Array<{id: number, attr: number}>();
    if(!str) return result;
    let decodeArr = decodeArrayListStr(str);
    for(let [id, attr] of decodeArr) {
        if(isNaN(parseInt(id)) || isNaN(parseInt(attr))) {
            throw new Error('data table format wrong');
        }
        result.push({id: parseInt(id), attr: parseInt(attr)});
    }
    return result
}