import { GraphElement } from '@csnext/cs-data';
export class EntityParser {
    constructor() {
        this.id = 'EntityParser';
        this.title = 'Entity Parser';
        this.description = this.title;
        this.options = {};
    }
    callDocument(doc, source) {
        return new Promise((resolve, reject) => {
            var _a, _b, _c, _d, _e, _f, _g;
            if (doc.entities && source) {
                let ids = [];
                source.viewTypes = {};
                let containingEntities = (_a = doc._outgoing) === null || _a === void 0 ? void 0 : _a.filter(o => o.classId === 'CONTAINS');
                if (containingEntities && doc.entities) {
                    ids = [...new Set(containingEntities.map(o => o.toId))];
                    for (const entityEdge of containingEntities) {
                        const entity = doc.entities.find(i => i.node_id === entityEdge.toId);
                        if (entity) {
                            entity._edge = entityEdge;
                            entity._node = entityEdge.to;
                        }
                    }
                }
                for (const entity of doc.entities) {
                    entity._included = (entity.id !== undefined) && ids.includes(entity.node_id);
                    if ((_c = (_b = entity._node) === null || _b === void 0 ? void 0 : _b._featureType) === null || _c === void 0 ? void 0 : _c.type) {
                        entity.class = entity._node._featureType.type;
                        if (!source.viewTypes.hasOwnProperty(entity.class)) {
                            const color = GraphElement.getBackgroundColor(entity._node);
                            source.viewTypes[entity.class] = { id: entity.class, title: entity._node._featureType.title, color, _selected: true };
                        }
                    }
                    else if (entity.entity_class) {
                        entity.class = entity.entity_class;
                        if (entity.class && !source.viewTypes.hasOwnProperty(entity.class)) {
                            source.viewTypes[entity.class] = { id: entity.class, title: entity.entity_class, color: 'lightgray', _selected: false };
                        }
                    }
                    if ((_e = (_d = entity._node) === null || _d === void 0 ? void 0 : _d.properties) === null || _e === void 0 ? void 0 : _e.location) {
                        entity._location = entity._node.properties.location;
                    }
                    else if (entity.entity_class === 'location' && entity.converted) {
                        entity._location = entity.converted;
                    }
                    if ((_g = (_f = entity._node) === null || _f === void 0 ? void 0 : _f.properties) === null || _g === void 0 ? void 0 : _g.point_in_time) {
                        entity._date = entity._node.properties.point_in_time;
                    }
                    else if (entity.entity_class === 'DATE' && entity.converted) {
                        entity._date = entity.converted;
                    }
                }
            }
            resolve({ document: doc });
        });
    }
}
//# sourceMappingURL=EntityParser.js.map