/**
 * 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 { SerializationFactory, usingModelSchema, } from '@finos/legend-shared';
import { createModelSchema, custom, deserialize, list, optional, primitive, serialize, } from 'serializr';
import { EntityMappedProperty, EnumMappedProperty, MappedEntity, MappedEntityInfo, MappedProperty, MappingModelCoverageAnalysisResult, } from '../../../../../../graph-manager/action/analytics/MappingModelCoverageAnalysis.js';
import { V1_deserializePureModelContextData, V1_pureModelContextPropSchema, V1_serializePureModelContextData, } from '../../transformation/pureProtocol/V1_PureProtocolSerialization.js';
var V1_MappedPropertyType;
(function (V1_MappedPropertyType) {
    V1_MappedPropertyType["ENUM"] = "enum";
    V1_MappedPropertyType["ENTITY"] = "entity";
})(V1_MappedPropertyType || (V1_MappedPropertyType = {}));
class V1_MappedProperty {
    name;
    static serialization = new SerializationFactory(createModelSchema(V1_MappedProperty, {
        name: primitive(),
    }));
}
class V1_EntityMappedProperty extends V1_MappedProperty {
    entityPath;
    subType;
    static serialization = new SerializationFactory(createModelSchema(V1_EntityMappedProperty, {
        name: primitive(),
        entityPath: primitive(),
        subType: optional(primitive()),
    }));
}
class V1_EnumMappedProperty extends V1_MappedProperty {
    enumPath;
    static serialization = new SerializationFactory(createModelSchema(V1_EnumMappedProperty, {
        name: primitive(),
        enumPath: primitive(),
    }));
}
const V1_serializeMappedProperty = (prop) => V1_MappedProperty instanceof V1_EnumMappedProperty
    ? serialize(V1_EnumMappedProperty.serialization.schema, prop)
    : V1_MappedProperty instanceof V1_EntityMappedProperty
        ? serialize(V1_EntityMappedProperty.serialization.schema, prop)
        : serialize(V1_MappedProperty.serialization.schema, prop);
const V1_deserializeMappedProperty = (json) => {
    switch (json._type) {
        case V1_MappedPropertyType.ENTITY:
            return deserialize(V1_EntityMappedProperty.serialization.schema, json);
        case V1_MappedPropertyType.ENUM:
            return deserialize(V1_EnumMappedProperty.serialization.schema, json);
        default: {
            // fix: data product artifacts omit type
            if (json.entityPath !== undefined) {
                return deserialize(V1_EntityMappedProperty.serialization.schema, json);
            }
            if (json.enumPath !== undefined) {
                return deserialize(V1_EnumMappedProperty.serialization.schema, json);
            }
            return deserialize(V1_MappedProperty.serialization.schema, json);
        }
    }
};
class V1_MappedEntityInfo {
    classPath;
    isRootEntity;
    subClasses = [];
    static serialization = new SerializationFactory(createModelSchema(V1_MappedEntityInfo, {
        classPath: primitive(),
        isRootEntity: primitive(),
        subClasses: list(primitive()),
    }));
}
export class V1_MappedEntity {
    path;
    properties = [];
    info;
    static serialization = new SerializationFactory(createModelSchema(V1_MappedEntity, {
        path: primitive(),
        info: optional(usingModelSchema(V1_MappedEntityInfo.serialization.schema)),
        properties: list(custom((prop) => V1_serializeMappedProperty(prop), (prop) => V1_deserializeMappedProperty(prop))),
    }));
}
export class V1_MappingModelCoverageAnalysisInput {
    clientVersion;
    mapping;
    model;
    static serialization = new SerializationFactory(createModelSchema(V1_MappingModelCoverageAnalysisInput, {
        clientVersion: primitive(),
        mapping: primitive(),
        model: V1_pureModelContextPropSchema,
    }));
}
export class V1_MappingModelCoverageAnalysisResult {
    mappedEntities = [];
    model;
    static serialization = new SerializationFactory(createModelSchema(V1_MappingModelCoverageAnalysisResult, {
        mappedEntities: list(usingModelSchema(V1_MappedEntity.serialization.schema)),
        model: optional(custom((val) => V1_serializePureModelContextData(val), (val) => V1_deserializePureModelContextData(val))),
    }));
}
const buildMappedProperty = (protocol) => protocol instanceof V1_EntityMappedProperty
    ? new EntityMappedProperty(protocol.name, protocol.entityPath, protocol.subType)
    : protocol instanceof V1_EnumMappedProperty
        ? new EnumMappedProperty(protocol.name, protocol.enumPath)
        : new MappedProperty(protocol.name);
const buildMappedEntityInfo = (protocol) => new MappedEntityInfo(protocol.classPath, protocol.isRootEntity, protocol.subClasses);
const buildMappedEntity = (protocol) => {
    const info = protocol.info ? buildMappedEntityInfo(protocol.info) : undefined;
    return new MappedEntity(protocol.path, protocol.properties.map((p) => buildMappedProperty(p)), info);
};
export const V1_buildModelCoverageAnalysisResult = (protocol, graphManager, mapping, pmcd) => {
    const entities = (pmcd ? pmcd : protocol.model)?.elements.map((el) => graphManager.elementProtocolToEntity(el));
    return new MappingModelCoverageAnalysisResult(protocol.mappedEntities.map((p) => buildMappedEntity(p)), mapping, entities);
};
//# sourceMappingURL=V1_MappingModelCoverageAnalysis.js.map