import { default as MicroEditorModel } from "./MicroEditorModel.js";
import { default as MacroEditorModel } from "./MacroEditorModel.js";
export default class EditorCollection {
    constructor(data) {
        this.m_data = {};
        this.m_source = data;
    }
    get data() {
        return this.m_data;
    }
    get source() {
        return this.m_source;
    }
    getEditorsByFeature(feature) {
        let result = [];
        let groupId = feature.parent.id;
        let featureId = feature.id;
        this.loadEditors(feature);
        let groupExists = this.data[groupId] != undefined;
        let featureExists = this.data[groupId][featureId] != null;
        if (groupExists && featureExists) {
            result = this.data[groupId][featureId];
        }
        return result;
    }
    getEditorsByGroup(group) {
        let result = [];
        if (this.data[group.id] == null) {
            group.features.forEach(feature => {
                let editors = this.getEditorsByFeature(feature);
                result = result.concat(editors);
            });
        }
        result = this.sortEditors(result);
        return result;
    }
    toJSON() {
        return this.source;
    }
    loadEditors(targetFeature) {
        let groupId = targetFeature.parent.id;
        let featureId = targetFeature.binding;
        let feature = null;
        let editors = [];
        if (this.data[groupId] == undefined) {
            feature = this.data[groupId] = {};
        }
        if (this.data[groupId][featureId] == undefined) {
            editors = this.data[groupId][featureId] = [];
        }
        if (editors.length > 0) {
            return;
        }
        Object.keys(this.source).forEach(key => {
            this.source[key].forEach(item => {
                if (item.type == targetFeature.binding) {
                    let result = null;
                    if (MicroEditorModel.isMicroEditor(item)) {
                        result = new MicroEditorModel({
                            data: item,
                            feature: targetFeature
                        });
                    }
                    else {
                        result = new MacroEditorModel({
                            data: item,
                            feature: targetFeature
                        });
                    }
                    editors[editors.length] = result;
                }
            });
        });
    }
    sortEditors(editors) {
        let result = editors.slice(0);
        let padding = (value) => {
            return Jade.String.padLeft(value, "0", 10);
        };
        result.sort((a, b) => {
            let aSortValue = "";
            let bSortValue = "";
            if (a instanceof MicroEditorModel) {
                aSortValue = `${a.category}_${padding(a.order)}_${a.label}`;
            }
            if (b instanceof MicroEditorModel) {
                bSortValue = `${b.category}_${padding(b.order)}_${b.label}`;
            }
            return aSortValue.toLowerCase().localeCompare(bSortValue.toLowerCase());
        });
        return result;
    }
}
