// Code generated by Speakeasy (https://speakeasy.com). DO NOT EDIT.

package provider

import (
	"encoding/json"
	tfTypes "github.com/airbytehq/terraform-provider-airbyte/internal/provider/types"
	"github.com/airbytehq/terraform-provider-airbyte/internal/sdk/models/shared"
	"github.com/hashicorp/terraform-plugin-framework/types"
)

func (r *DestinationS3DataLakeResourceModel) ToSharedDestinationS3DataLakeCreateRequest() *shared.DestinationS3DataLakeCreateRequest {
	var name string
	name = r.Name.ValueString()

	definitionID := new(string)
	if !r.DefinitionID.IsUnknown() && !r.DefinitionID.IsNull() {
		*definitionID = r.DefinitionID.ValueString()
	} else {
		definitionID = nil
	}
	var workspaceID string
	workspaceID = r.WorkspaceID.ValueString()

	accessKeyID := new(string)
	if !r.Configuration.AccessKeyID.IsUnknown() && !r.Configuration.AccessKeyID.IsNull() {
		*accessKeyID = r.Configuration.AccessKeyID.ValueString()
	} else {
		accessKeyID = nil
	}
	var catalogType shared.CatalogType
	var nessieCatalog *shared.NessieCatalog
	if r.Configuration.CatalogType.NessieCatalog != nil {
		accessToken := new(string)
		if !r.Configuration.CatalogType.NessieCatalog.AccessToken.IsUnknown() && !r.Configuration.CatalogType.NessieCatalog.AccessToken.IsNull() {
			*accessToken = r.Configuration.CatalogType.NessieCatalog.AccessToken.ValueString()
		} else {
			accessToken = nil
		}
		catalogType1 := new(shared.DestinationS3DataLakeSchemasCatalogTypeCatalogType)
		if !r.Configuration.CatalogType.NessieCatalog.CatalogType.IsUnknown() && !r.Configuration.CatalogType.NessieCatalog.CatalogType.IsNull() {
			*catalogType1 = shared.DestinationS3DataLakeSchemasCatalogTypeCatalogType(r.Configuration.CatalogType.NessieCatalog.CatalogType.ValueString())
		} else {
			catalogType1 = nil
		}
		var namespace string
		namespace = r.Configuration.CatalogType.NessieCatalog.Namespace.ValueString()

		var serverURI string
		serverURI = r.Configuration.CatalogType.NessieCatalog.ServerURI.ValueString()

		var additionalProperties interface{}
		if !r.Configuration.CatalogType.NessieCatalog.AdditionalProperties.IsUnknown() && !r.Configuration.CatalogType.NessieCatalog.AdditionalProperties.IsNull() {
			_ = json.Unmarshal([]byte(r.Configuration.CatalogType.NessieCatalog.AdditionalProperties.ValueString()), &additionalProperties)
		}
		nessieCatalog = &shared.NessieCatalog{
			AccessToken:          accessToken,
			CatalogType:          catalogType1,
			Namespace:            namespace,
			ServerURI:            serverURI,
			AdditionalProperties: additionalProperties,
		}
	}
	if nessieCatalog != nil {
		catalogType = shared.CatalogType{
			NessieCatalog: nessieCatalog,
		}
	}
	var glueCatalog *shared.GlueCatalog
	if r.Configuration.CatalogType.GlueCatalog != nil {
		catalogType2 := new(shared.DestinationS3DataLakeCatalogType)
		if !r.Configuration.CatalogType.GlueCatalog.CatalogType.IsUnknown() && !r.Configuration.CatalogType.GlueCatalog.CatalogType.IsNull() {
			*catalogType2 = shared.DestinationS3DataLakeCatalogType(r.Configuration.CatalogType.GlueCatalog.CatalogType.ValueString())
		} else {
			catalogType2 = nil
		}
		var databaseName string
		databaseName = r.Configuration.CatalogType.GlueCatalog.DatabaseName.ValueString()

		var glueID string
		glueID = r.Configuration.CatalogType.GlueCatalog.GlueID.ValueString()

		roleArn := new(string)
		if !r.Configuration.CatalogType.GlueCatalog.RoleArn.IsUnknown() && !r.Configuration.CatalogType.GlueCatalog.RoleArn.IsNull() {
			*roleArn = r.Configuration.CatalogType.GlueCatalog.RoleArn.ValueString()
		} else {
			roleArn = nil
		}
		var additionalProperties1 interface{}
		if !r.Configuration.CatalogType.GlueCatalog.AdditionalProperties.IsUnknown() && !r.Configuration.CatalogType.GlueCatalog.AdditionalProperties.IsNull() {
			_ = json.Unmarshal([]byte(r.Configuration.CatalogType.GlueCatalog.AdditionalProperties.ValueString()), &additionalProperties1)
		}
		glueCatalog = &shared.GlueCatalog{
			CatalogType:          catalogType2,
			DatabaseName:         databaseName,
			GlueID:               glueID,
			RoleArn:              roleArn,
			AdditionalProperties: additionalProperties1,
		}
	}
	if glueCatalog != nil {
		catalogType = shared.CatalogType{
			GlueCatalog: glueCatalog,
		}
	}
	var restCatalog *shared.RestCatalog
	if r.Configuration.CatalogType.RestCatalog != nil {
		catalogType3 := new(shared.DestinationS3DataLakeSchemasCatalogType)
		if !r.Configuration.CatalogType.RestCatalog.CatalogType.IsUnknown() && !r.Configuration.CatalogType.RestCatalog.CatalogType.IsNull() {
			*catalogType3 = shared.DestinationS3DataLakeSchemasCatalogType(r.Configuration.CatalogType.RestCatalog.CatalogType.ValueString())
		} else {
			catalogType3 = nil
		}
		var namespace1 string
		namespace1 = r.Configuration.CatalogType.RestCatalog.Namespace.ValueString()

		var serverUri1 string
		serverUri1 = r.Configuration.CatalogType.RestCatalog.ServerURI.ValueString()

		var additionalProperties2 interface{}
		if !r.Configuration.CatalogType.RestCatalog.AdditionalProperties.IsUnknown() && !r.Configuration.CatalogType.RestCatalog.AdditionalProperties.IsNull() {
			_ = json.Unmarshal([]byte(r.Configuration.CatalogType.RestCatalog.AdditionalProperties.ValueString()), &additionalProperties2)
		}
		restCatalog = &shared.RestCatalog{
			CatalogType:          catalogType3,
			Namespace:            namespace1,
			ServerURI:            serverUri1,
			AdditionalProperties: additionalProperties2,
		}
	}
	if restCatalog != nil {
		catalogType = shared.CatalogType{
			RestCatalog: restCatalog,
		}
	}
	mainBranchName := new(string)
	if !r.Configuration.MainBranchName.IsUnknown() && !r.Configuration.MainBranchName.IsNull() {
		*mainBranchName = r.Configuration.MainBranchName.ValueString()
	} else {
		mainBranchName = nil
	}
	var s3BucketName string
	s3BucketName = r.Configuration.S3BucketName.ValueString()

	s3BucketRegion := shared.DestinationS3DataLakeS3BucketRegion(r.Configuration.S3BucketRegion.ValueString())
	s3Endpoint := new(string)
	if !r.Configuration.S3Endpoint.IsUnknown() && !r.Configuration.S3Endpoint.IsNull() {
		*s3Endpoint = r.Configuration.S3Endpoint.ValueString()
	} else {
		s3Endpoint = nil
	}
	secretAccessKey := new(string)
	if !r.Configuration.SecretAccessKey.IsUnknown() && !r.Configuration.SecretAccessKey.IsNull() {
		*secretAccessKey = r.Configuration.SecretAccessKey.ValueString()
	} else {
		secretAccessKey = nil
	}
	var warehouseLocation string
	warehouseLocation = r.Configuration.WarehouseLocation.ValueString()

	configuration := shared.DestinationS3DataLake{
		AccessKeyID:       accessKeyID,
		CatalogType:       catalogType,
		MainBranchName:    mainBranchName,
		S3BucketName:      s3BucketName,
		S3BucketRegion:    s3BucketRegion,
		S3Endpoint:        s3Endpoint,
		SecretAccessKey:   secretAccessKey,
		WarehouseLocation: warehouseLocation,
	}
	out := shared.DestinationS3DataLakeCreateRequest{
		Name:          name,
		DefinitionID:  definitionID,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}

func (r *DestinationS3DataLakeResourceModel) RefreshFromSharedDestinationResponse(resp *shared.DestinationResponse) {
	if resp != nil {
		r.CreatedAt = types.Int64Value(resp.CreatedAt)
		r.DefinitionID = types.StringValue(resp.DefinitionID)
		r.DestinationID = types.StringValue(resp.DestinationID)
		r.DestinationType = types.StringValue(resp.DestinationType)
		r.Name = types.StringValue(resp.Name)
		if resp.ResourceAllocation == nil {
			r.ResourceAllocation = nil
		} else {
			r.ResourceAllocation = &tfTypes.ScopedResourceRequirements{}
			if resp.ResourceAllocation.Default == nil {
				r.ResourceAllocation.Default = nil
			} else {
				r.ResourceAllocation.Default = &tfTypes.ResourceRequirements{}
				r.ResourceAllocation.Default.CPULimit = types.StringPointerValue(resp.ResourceAllocation.Default.CPULimit)
				r.ResourceAllocation.Default.CPURequest = types.StringPointerValue(resp.ResourceAllocation.Default.CPURequest)
				r.ResourceAllocation.Default.EphemeralStorageLimit = types.StringPointerValue(resp.ResourceAllocation.Default.EphemeralStorageLimit)
				r.ResourceAllocation.Default.EphemeralStorageRequest = types.StringPointerValue(resp.ResourceAllocation.Default.EphemeralStorageRequest)
				r.ResourceAllocation.Default.MemoryLimit = types.StringPointerValue(resp.ResourceAllocation.Default.MemoryLimit)
				r.ResourceAllocation.Default.MemoryRequest = types.StringPointerValue(resp.ResourceAllocation.Default.MemoryRequest)
			}
			r.ResourceAllocation.JobSpecific = []tfTypes.JobTypeResourceLimit{}
			if len(r.ResourceAllocation.JobSpecific) > len(resp.ResourceAllocation.JobSpecific) {
				r.ResourceAllocation.JobSpecific = r.ResourceAllocation.JobSpecific[:len(resp.ResourceAllocation.JobSpecific)]
			}
			for jobSpecificCount, jobSpecificItem := range resp.ResourceAllocation.JobSpecific {
				var jobSpecific1 tfTypes.JobTypeResourceLimit
				jobSpecific1.JobType = types.StringValue(string(jobSpecificItem.JobType))
				jobSpecific1.ResourceRequirements.CPULimit = types.StringPointerValue(jobSpecificItem.ResourceRequirements.CPULimit)
				jobSpecific1.ResourceRequirements.CPURequest = types.StringPointerValue(jobSpecificItem.ResourceRequirements.CPURequest)
				jobSpecific1.ResourceRequirements.EphemeralStorageLimit = types.StringPointerValue(jobSpecificItem.ResourceRequirements.EphemeralStorageLimit)
				jobSpecific1.ResourceRequirements.EphemeralStorageRequest = types.StringPointerValue(jobSpecificItem.ResourceRequirements.EphemeralStorageRequest)
				jobSpecific1.ResourceRequirements.MemoryLimit = types.StringPointerValue(jobSpecificItem.ResourceRequirements.MemoryLimit)
				jobSpecific1.ResourceRequirements.MemoryRequest = types.StringPointerValue(jobSpecificItem.ResourceRequirements.MemoryRequest)
				if jobSpecificCount+1 > len(r.ResourceAllocation.JobSpecific) {
					r.ResourceAllocation.JobSpecific = append(r.ResourceAllocation.JobSpecific, jobSpecific1)
				} else {
					r.ResourceAllocation.JobSpecific[jobSpecificCount].JobType = jobSpecific1.JobType
					r.ResourceAllocation.JobSpecific[jobSpecificCount].ResourceRequirements = jobSpecific1.ResourceRequirements
				}
			}
		}
		r.WorkspaceID = types.StringValue(resp.WorkspaceID)
	}
}

func (r *DestinationS3DataLakeResourceModel) ToSharedDestinationS3DataLakePutRequest() *shared.DestinationS3DataLakePutRequest {
	var name string
	name = r.Name.ValueString()

	var workspaceID string
	workspaceID = r.WorkspaceID.ValueString()

	accessKeyID := new(string)
	if !r.Configuration.AccessKeyID.IsUnknown() && !r.Configuration.AccessKeyID.IsNull() {
		*accessKeyID = r.Configuration.AccessKeyID.ValueString()
	} else {
		accessKeyID = nil
	}
	var catalogType shared.DestinationS3DataLakeUpdateCatalogType
	var destinationS3DataLakeUpdateNessieCatalog *shared.DestinationS3DataLakeUpdateNessieCatalog
	if r.Configuration.CatalogType.NessieCatalog != nil {
		accessToken := new(string)
		if !r.Configuration.CatalogType.NessieCatalog.AccessToken.IsUnknown() && !r.Configuration.CatalogType.NessieCatalog.AccessToken.IsNull() {
			*accessToken = r.Configuration.CatalogType.NessieCatalog.AccessToken.ValueString()
		} else {
			accessToken = nil
		}
		catalogType1 := new(shared.DestinationS3DataLakeUpdateSchemasCatalogTypeCatalogTypeCatalogType)
		if !r.Configuration.CatalogType.NessieCatalog.CatalogType.IsUnknown() && !r.Configuration.CatalogType.NessieCatalog.CatalogType.IsNull() {
			*catalogType1 = shared.DestinationS3DataLakeUpdateSchemasCatalogTypeCatalogTypeCatalogType(r.Configuration.CatalogType.NessieCatalog.CatalogType.ValueString())
		} else {
			catalogType1 = nil
		}
		var namespace string
		namespace = r.Configuration.CatalogType.NessieCatalog.Namespace.ValueString()

		var serverURI string
		serverURI = r.Configuration.CatalogType.NessieCatalog.ServerURI.ValueString()

		var additionalProperties interface{}
		if !r.Configuration.CatalogType.NessieCatalog.AdditionalProperties.IsUnknown() && !r.Configuration.CatalogType.NessieCatalog.AdditionalProperties.IsNull() {
			_ = json.Unmarshal([]byte(r.Configuration.CatalogType.NessieCatalog.AdditionalProperties.ValueString()), &additionalProperties)
		}
		destinationS3DataLakeUpdateNessieCatalog = &shared.DestinationS3DataLakeUpdateNessieCatalog{
			AccessToken:          accessToken,
			CatalogType:          catalogType1,
			Namespace:            namespace,
			ServerURI:            serverURI,
			AdditionalProperties: additionalProperties,
		}
	}
	if destinationS3DataLakeUpdateNessieCatalog != nil {
		catalogType = shared.DestinationS3DataLakeUpdateCatalogType{
			DestinationS3DataLakeUpdateNessieCatalog: destinationS3DataLakeUpdateNessieCatalog,
		}
	}
	var destinationS3DataLakeUpdateGlueCatalog *shared.DestinationS3DataLakeUpdateGlueCatalog
	if r.Configuration.CatalogType.GlueCatalog != nil {
		catalogType2 := new(shared.DestinationS3DataLakeUpdateSchemasCatalogType)
		if !r.Configuration.CatalogType.GlueCatalog.CatalogType.IsUnknown() && !r.Configuration.CatalogType.GlueCatalog.CatalogType.IsNull() {
			*catalogType2 = shared.DestinationS3DataLakeUpdateSchemasCatalogType(r.Configuration.CatalogType.GlueCatalog.CatalogType.ValueString())
		} else {
			catalogType2 = nil
		}
		var databaseName string
		databaseName = r.Configuration.CatalogType.GlueCatalog.DatabaseName.ValueString()

		var glueID string
		glueID = r.Configuration.CatalogType.GlueCatalog.GlueID.ValueString()

		roleArn := new(string)
		if !r.Configuration.CatalogType.GlueCatalog.RoleArn.IsUnknown() && !r.Configuration.CatalogType.GlueCatalog.RoleArn.IsNull() {
			*roleArn = r.Configuration.CatalogType.GlueCatalog.RoleArn.ValueString()
		} else {
			roleArn = nil
		}
		var additionalProperties1 interface{}
		if !r.Configuration.CatalogType.GlueCatalog.AdditionalProperties.IsUnknown() && !r.Configuration.CatalogType.GlueCatalog.AdditionalProperties.IsNull() {
			_ = json.Unmarshal([]byte(r.Configuration.CatalogType.GlueCatalog.AdditionalProperties.ValueString()), &additionalProperties1)
		}
		destinationS3DataLakeUpdateGlueCatalog = &shared.DestinationS3DataLakeUpdateGlueCatalog{
			CatalogType:          catalogType2,
			DatabaseName:         databaseName,
			GlueID:               glueID,
			RoleArn:              roleArn,
			AdditionalProperties: additionalProperties1,
		}
	}
	if destinationS3DataLakeUpdateGlueCatalog != nil {
		catalogType = shared.DestinationS3DataLakeUpdateCatalogType{
			DestinationS3DataLakeUpdateGlueCatalog: destinationS3DataLakeUpdateGlueCatalog,
		}
	}
	var destinationS3DataLakeUpdateRestCatalog *shared.DestinationS3DataLakeUpdateRestCatalog
	if r.Configuration.CatalogType.RestCatalog != nil {
		catalogType3 := new(shared.DestinationS3DataLakeUpdateSchemasCatalogTypeCatalogType)
		if !r.Configuration.CatalogType.RestCatalog.CatalogType.IsUnknown() && !r.Configuration.CatalogType.RestCatalog.CatalogType.IsNull() {
			*catalogType3 = shared.DestinationS3DataLakeUpdateSchemasCatalogTypeCatalogType(r.Configuration.CatalogType.RestCatalog.CatalogType.ValueString())
		} else {
			catalogType3 = nil
		}
		var namespace1 string
		namespace1 = r.Configuration.CatalogType.RestCatalog.Namespace.ValueString()

		var serverUri1 string
		serverUri1 = r.Configuration.CatalogType.RestCatalog.ServerURI.ValueString()

		var additionalProperties2 interface{}
		if !r.Configuration.CatalogType.RestCatalog.AdditionalProperties.IsUnknown() && !r.Configuration.CatalogType.RestCatalog.AdditionalProperties.IsNull() {
			_ = json.Unmarshal([]byte(r.Configuration.CatalogType.RestCatalog.AdditionalProperties.ValueString()), &additionalProperties2)
		}
		destinationS3DataLakeUpdateRestCatalog = &shared.DestinationS3DataLakeUpdateRestCatalog{
			CatalogType:          catalogType3,
			Namespace:            namespace1,
			ServerURI:            serverUri1,
			AdditionalProperties: additionalProperties2,
		}
	}
	if destinationS3DataLakeUpdateRestCatalog != nil {
		catalogType = shared.DestinationS3DataLakeUpdateCatalogType{
			DestinationS3DataLakeUpdateRestCatalog: destinationS3DataLakeUpdateRestCatalog,
		}
	}
	mainBranchName := new(string)
	if !r.Configuration.MainBranchName.IsUnknown() && !r.Configuration.MainBranchName.IsNull() {
		*mainBranchName = r.Configuration.MainBranchName.ValueString()
	} else {
		mainBranchName = nil
	}
	var s3BucketName string
	s3BucketName = r.Configuration.S3BucketName.ValueString()

	s3BucketRegion := shared.DestinationS3DataLakeUpdateS3BucketRegion(r.Configuration.S3BucketRegion.ValueString())
	s3Endpoint := new(string)
	if !r.Configuration.S3Endpoint.IsUnknown() && !r.Configuration.S3Endpoint.IsNull() {
		*s3Endpoint = r.Configuration.S3Endpoint.ValueString()
	} else {
		s3Endpoint = nil
	}
	secretAccessKey := new(string)
	if !r.Configuration.SecretAccessKey.IsUnknown() && !r.Configuration.SecretAccessKey.IsNull() {
		*secretAccessKey = r.Configuration.SecretAccessKey.ValueString()
	} else {
		secretAccessKey = nil
	}
	var warehouseLocation string
	warehouseLocation = r.Configuration.WarehouseLocation.ValueString()

	configuration := shared.DestinationS3DataLakeUpdate{
		AccessKeyID:       accessKeyID,
		CatalogType:       catalogType,
		MainBranchName:    mainBranchName,
		S3BucketName:      s3BucketName,
		S3BucketRegion:    s3BucketRegion,
		S3Endpoint:        s3Endpoint,
		SecretAccessKey:   secretAccessKey,
		WarehouseLocation: warehouseLocation,
	}
	out := shared.DestinationS3DataLakePutRequest{
		Name:          name,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}
