/**
 * 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, usingConstantValueSchema, usingModelSchema, } from '@finos/legend-shared';
import { V1_IngestDefinition } from '../../../model/packageableElements/ingest/V1_IngestDefinition.js';
import { V1_AppDirNodeModelSchema } from './V1_EntitlementSerializationHelper.js';
import { createModelSchema, custom, deserialize, primitive, serialize, } from 'serializr';
import { V1_AWSSnowflakeIngestEnvironment, V1_CatalogType, V1_IcebergDetails, V1_IngestEnvironmentType, V1_OpenCatalog, } from '../../../lakehouse/ingest/V1_LakehouseIngestEnvironment.js';
import { V1_AWSSnowflakeProducerEnvironment, V1_ProducerEnvironmentType, } from '../../../lakehouse/ingest/V1_LakehouseProducerEnvironment.js';
export const V1_createIngestDef = (name, packagePath, json) => {
    const ingestDef = new V1_IngestDefinition();
    const jsonType = json;
    const appDir = jsonType.appDirDeployment ??
        jsonType.owner?.prodParallel ??
        jsonType.owner?.production;
    ingestDef.name = name;
    ingestDef.appDirDeployment = appDir
        ? deserialize(V1_AppDirNodeModelSchema, appDir)
        : undefined;
    ingestDef.package = packagePath;
    ingestDef.content = json;
    return ingestDef;
};
export const V1_OpenCatalogModelSchema = createModelSchema(V1_OpenCatalog, {
    name: primitive(),
    url: primitive(),
    proxyUrl: primitive(),
    _type: usingConstantValueSchema(V1_CatalogType.OpenCatalog),
});
export const V1_deserializeCatalog = (json) => {
    switch (json._type) {
        case V1_CatalogType.OpenCatalog:
            return deserialize(V1_OpenCatalogModelSchema, json);
        default:
            throw new Error(`Unknown V1_Catalog type: ${json._type}`);
    }
};
export const V1_serializeCatalog = (protocol) => {
    if (protocol instanceof V1_OpenCatalog) {
        return serialize(V1_OpenCatalogModelSchema, protocol);
    }
    throw new UnsupportedOperationError(`Unknown V1_Catalog type:`, protocol);
};
export const V1_IcebergDetailsModelSchema = createModelSchema(V1_IcebergDetails, {
    catalog: custom((val) => V1_serializeCatalog(val), (val) => V1_deserializeCatalog(val)),
});
export const V1_AWSSnowflakeIngestEnvironmentModelSchema = createModelSchema(V1_AWSSnowflakeIngestEnvironment, {
    _type: usingConstantValueSchema(V1_IngestEnvironmentType.AWSSnowflake),
    urn: primitive(),
    version: primitive(),
    environmentClassification: primitive(),
    producers: usingModelSchema(V1_AppDirNodeModelSchema),
    awsRegion: primitive(),
    awsAccountId: primitive(),
    ingestStepFunctionsAvtivityArn: primitive(),
    ingestStateMachineArn: primitive(),
    ingestSystemAccount: primitive(),
    snowflakeAccount: primitive(),
    snowflakeHost: primitive(),
    s3StagingBucketName: primitive(),
    storageIntegrationName: primitive(),
    iceberg: usingModelSchema(V1_IcebergDetailsModelSchema),
});
export const V1_deserializeIngestEnvironment = (json) => {
    switch (json._type) {
        case V1_IngestEnvironmentType.AWSSnowflake:
            return deserialize(V1_AWSSnowflakeIngestEnvironmentModelSchema, json);
        default:
            throw new Error(`Unknown V1_IngestEnvironment type: ${json._type}`);
    }
};
export const V1_AWSSnowflakeProducerEnvironmentModelSchema = createModelSchema(V1_AWSSnowflakeProducerEnvironment, {
    _type: primitive(),
    appDirDeployment: usingModelSchema(V1_AppDirNodeModelSchema),
    snowflakeRole: primitive(),
    databaseName: primitive(),
    warehouseName: primitive(),
    stageName: primitive(),
    icebergEnabled: primitive(),
    databaseOwnerDeploymentId: primitive(),
});
export const V1_deserializeProducerEnvironment = (json) => {
    switch (json._type) {
        case V1_ProducerEnvironmentType.AWSSnowflake:
        case V1_ProducerEnvironmentType.AWSSnowflakeWithOnPremS3Only:
        case V1_ProducerEnvironmentType.AWSSnowflakeWithProducerManagedStageBucket:
            return deserialize(V1_AWSSnowflakeProducerEnvironmentModelSchema, json);
        default:
            throw new Error(`Unknown V1_ProducerEnvironment type: ${json._type}`);
    }
};
//# sourceMappingURL=V1_IngestSerializationHelper.js.map