/**
 * Copyright (c) 2020-present, Goldman Sachs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { UnsupportedOperationError } from '@finos/legend-shared';
import { DataElementReference, ExternalFormatData, ModelStoreData, ModelEmbeddedData, ModelInstanceData, RelationElementsData, RelationElement, RelationRowTestData, } from '../../../../../../../../graph/metamodel/pure/data/EmbeddedData.js';
import { RelationalCSVData, RelationalCSVDataTable, } from '../../../../../../../../graph/metamodel/pure/data/RelationalCSVData.js';
import { V1_ModelInstanceData, V1_ModelEmbeddedData, } from '../../../../model/data/V1_EmbeddedData.js';
import { INTERNAL__UnknownEmbeddedData } from '../../../../../../../../graph/metamodel/pure/data/INTERNAL__UnknownEmbeddedData.js';
class V1_EmbeddedDataBuilder {
    context;
    constructor(context) {
        this.context = context;
    }
    visit_EmbeddedData(embeddedData) {
        const extraEmbeddedDataBuilders = this.context.extensions.plugins.flatMap((plugin) => plugin.V1_getExtraEmbeddedDataBuilders?.() ?? []);
        for (const builder of extraEmbeddedDataBuilders) {
            const metamodel = builder(embeddedData, this.context);
            if (metamodel) {
                return metamodel;
            }
        }
        throw new UnsupportedOperationError(`Can't build embedded data: no compatible builder available from plugins`, embeddedData);
    }
    visit_INTERNAL__UnknownEmbeddedData(data) {
        const metamodel = new INTERNAL__UnknownEmbeddedData();
        metamodel.content = data.content;
        return metamodel;
    }
    visit_ExternalFormatData(externalFormatData) {
        const metamodel = new ExternalFormatData();
        metamodel.contentType = externalFormatData.contentType;
        metamodel.data = externalFormatData.data;
        return metamodel;
    }
    visit_RelationElementsData(data) {
        const metamodel = new RelationElementsData();
        metamodel.relationElements = data.relationElements.map((relationElement) => {
            const v = new RelationElement();
            v.columns = relationElement.columns;
            v.paths = relationElement.paths;
            v.rows = relationElement.rows.map((row) => {
                const r = new RelationRowTestData();
                r.values = row.values;
                return r;
            });
            return v;
        });
        return metamodel;
    }
    visit_ModelStoreData(modelStoreData) {
        const metamodel = new ModelStoreData();
        if (modelStoreData.modelData?.length) {
            metamodel.modelData = modelStoreData.modelData.map((modelData) => {
                if (modelData instanceof V1_ModelInstanceData) {
                    const val = new ModelInstanceData();
                    val.model = this.context.resolveClass(modelData.model);
                    val.instances = modelData.instances;
                    return val;
                }
                else if (modelData instanceof V1_ModelEmbeddedData) {
                    const val = new ModelEmbeddedData();
                    val.data = V1_buildEmbeddedData(modelData.data, this.context);
                    val.model = this.context.resolveClass(modelData.model);
                    return val;
                }
                throw new UnsupportedOperationError('Model Data Type not supported');
            });
        }
        return metamodel;
    }
    visit_DataElementReference(dataElementReference) {
        const metamodel = new DataElementReference();
        metamodel.dataElement = this.context.resolveElement(dataElementReference.dataElement.path, false);
        return metamodel;
    }
    visit_RelationalData(relationalData) {
        const metamodel = new RelationalCSVData();
        metamodel.tables = relationalData.tables.map((t) => {
            const table = new RelationalCSVDataTable();
            table.schema = t.schema;
            table.table = t.table;
            table.values = t.values;
            return table;
        });
        return metamodel;
    }
}
export function V1_buildEmbeddedData(protocol, context) {
    return protocol.accept_EmbeddedDataVisitor(new V1_EmbeddedDataBuilder(context));
}
//# sourceMappingURL=V1_DataElementBuilderHelper.js.map