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

package provider

import (
	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 *DestinationElasticsearchResourceModel) ToSharedDestinationElasticsearchCreateRequest() *shared.DestinationElasticsearchCreateRequest {
	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()

	var authenticationMethod *shared.DestinationElasticsearchAuthenticationMethod
	if r.Configuration.AuthenticationMethod != nil {
		var destinationElasticsearchNone *shared.DestinationElasticsearchNone
		if r.Configuration.AuthenticationMethod.None != nil {
			destinationElasticsearchNone = &shared.DestinationElasticsearchNone{}
		}
		if destinationElasticsearchNone != nil {
			authenticationMethod = &shared.DestinationElasticsearchAuthenticationMethod{
				DestinationElasticsearchNone: destinationElasticsearchNone,
			}
		}
		var destinationElasticsearchAPIKeySecret *shared.DestinationElasticsearchAPIKeySecret
		if r.Configuration.AuthenticationMethod.APIKeySecret != nil {
			var apiKeyID string
			apiKeyID = r.Configuration.AuthenticationMethod.APIKeySecret.APIKeyID.ValueString()

			var apiKeySecret string
			apiKeySecret = r.Configuration.AuthenticationMethod.APIKeySecret.APIKeySecret.ValueString()

			destinationElasticsearchAPIKeySecret = &shared.DestinationElasticsearchAPIKeySecret{
				APIKeyID:     apiKeyID,
				APIKeySecret: apiKeySecret,
			}
		}
		if destinationElasticsearchAPIKeySecret != nil {
			authenticationMethod = &shared.DestinationElasticsearchAuthenticationMethod{
				DestinationElasticsearchAPIKeySecret: destinationElasticsearchAPIKeySecret,
			}
		}
		var destinationElasticsearchUsernamePassword *shared.DestinationElasticsearchUsernamePassword
		if r.Configuration.AuthenticationMethod.UsernamePassword != nil {
			var password string
			password = r.Configuration.AuthenticationMethod.UsernamePassword.Password.ValueString()

			var username string
			username = r.Configuration.AuthenticationMethod.UsernamePassword.Username.ValueString()

			destinationElasticsearchUsernamePassword = &shared.DestinationElasticsearchUsernamePassword{
				Password: password,
				Username: username,
			}
		}
		if destinationElasticsearchUsernamePassword != nil {
			authenticationMethod = &shared.DestinationElasticsearchAuthenticationMethod{
				DestinationElasticsearchUsernamePassword: destinationElasticsearchUsernamePassword,
			}
		}
	}
	caCertificate := new(string)
	if !r.Configuration.CaCertificate.IsUnknown() && !r.Configuration.CaCertificate.IsNull() {
		*caCertificate = r.Configuration.CaCertificate.ValueString()
	} else {
		caCertificate = nil
	}
	var endpoint string
	endpoint = r.Configuration.Endpoint.ValueString()

	pathPrefix := new(string)
	if !r.Configuration.PathPrefix.IsUnknown() && !r.Configuration.PathPrefix.IsNull() {
		*pathPrefix = r.Configuration.PathPrefix.ValueString()
	} else {
		pathPrefix = nil
	}
	var tunnelMethod *shared.DestinationElasticsearchSSHTunnelMethod
	if r.Configuration.TunnelMethod != nil {
		var destinationElasticsearchNoTunnel *shared.DestinationElasticsearchNoTunnel
		if r.Configuration.TunnelMethod.NoTunnel != nil {
			destinationElasticsearchNoTunnel = &shared.DestinationElasticsearchNoTunnel{}
		}
		if destinationElasticsearchNoTunnel != nil {
			tunnelMethod = &shared.DestinationElasticsearchSSHTunnelMethod{
				DestinationElasticsearchNoTunnel: destinationElasticsearchNoTunnel,
			}
		}
		var destinationElasticsearchSSHKeyAuthentication *shared.DestinationElasticsearchSSHKeyAuthentication
		if r.Configuration.TunnelMethod.SSHKeyAuthentication != nil {
			var sshKey string
			sshKey = r.Configuration.TunnelMethod.SSHKeyAuthentication.SSHKey.ValueString()

			var tunnelHost string
			tunnelHost = r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelHost.ValueString()

			tunnelPort := new(int64)
			if !r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelPort.IsUnknown() && !r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelPort.IsNull() {
				*tunnelPort = r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelPort.ValueInt64()
			} else {
				tunnelPort = nil
			}
			var tunnelUser string
			tunnelUser = r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelUser.ValueString()

			destinationElasticsearchSSHKeyAuthentication = &shared.DestinationElasticsearchSSHKeyAuthentication{
				SSHKey:     sshKey,
				TunnelHost: tunnelHost,
				TunnelPort: tunnelPort,
				TunnelUser: tunnelUser,
			}
		}
		if destinationElasticsearchSSHKeyAuthentication != nil {
			tunnelMethod = &shared.DestinationElasticsearchSSHTunnelMethod{
				DestinationElasticsearchSSHKeyAuthentication: destinationElasticsearchSSHKeyAuthentication,
			}
		}
		var destinationElasticsearchPasswordAuthentication *shared.DestinationElasticsearchPasswordAuthentication
		if r.Configuration.TunnelMethod.PasswordAuthentication != nil {
			var tunnelHost1 string
			tunnelHost1 = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelHost.ValueString()

			tunnelPort1 := new(int64)
			if !r.Configuration.TunnelMethod.PasswordAuthentication.TunnelPort.IsUnknown() && !r.Configuration.TunnelMethod.PasswordAuthentication.TunnelPort.IsNull() {
				*tunnelPort1 = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelPort.ValueInt64()
			} else {
				tunnelPort1 = nil
			}
			var tunnelUser1 string
			tunnelUser1 = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelUser.ValueString()

			var tunnelUserPassword string
			tunnelUserPassword = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelUserPassword.ValueString()

			destinationElasticsearchPasswordAuthentication = &shared.DestinationElasticsearchPasswordAuthentication{
				TunnelHost:         tunnelHost1,
				TunnelPort:         tunnelPort1,
				TunnelUser:         tunnelUser1,
				TunnelUserPassword: tunnelUserPassword,
			}
		}
		if destinationElasticsearchPasswordAuthentication != nil {
			tunnelMethod = &shared.DestinationElasticsearchSSHTunnelMethod{
				DestinationElasticsearchPasswordAuthentication: destinationElasticsearchPasswordAuthentication,
			}
		}
	}
	upsert := new(bool)
	if !r.Configuration.Upsert.IsUnknown() && !r.Configuration.Upsert.IsNull() {
		*upsert = r.Configuration.Upsert.ValueBool()
	} else {
		upsert = nil
	}
	configuration := shared.DestinationElasticsearch{
		AuthenticationMethod: authenticationMethod,
		CaCertificate:        caCertificate,
		Endpoint:             endpoint,
		PathPrefix:           pathPrefix,
		TunnelMethod:         tunnelMethod,
		Upsert:               upsert,
	}
	out := shared.DestinationElasticsearchCreateRequest{
		Name:          name,
		DefinitionID:  definitionID,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}

func (r *DestinationElasticsearchResourceModel) 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 *DestinationElasticsearchResourceModel) ToSharedDestinationElasticsearchPutRequest() *shared.DestinationElasticsearchPutRequest {
	var name string
	name = r.Name.ValueString()

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

	var authenticationMethod *shared.DestinationElasticsearchUpdateAuthenticationMethod
	if r.Configuration.AuthenticationMethod != nil {
		var destinationElasticsearchUpdateNone *shared.DestinationElasticsearchUpdateNone
		if r.Configuration.AuthenticationMethod.None != nil {
			destinationElasticsearchUpdateNone = &shared.DestinationElasticsearchUpdateNone{}
		}
		if destinationElasticsearchUpdateNone != nil {
			authenticationMethod = &shared.DestinationElasticsearchUpdateAuthenticationMethod{
				DestinationElasticsearchUpdateNone: destinationElasticsearchUpdateNone,
			}
		}
		var destinationElasticsearchUpdateAPIKeySecret *shared.DestinationElasticsearchUpdateAPIKeySecret
		if r.Configuration.AuthenticationMethod.APIKeySecret != nil {
			var apiKeyID string
			apiKeyID = r.Configuration.AuthenticationMethod.APIKeySecret.APIKeyID.ValueString()

			var apiKeySecret string
			apiKeySecret = r.Configuration.AuthenticationMethod.APIKeySecret.APIKeySecret.ValueString()

			destinationElasticsearchUpdateAPIKeySecret = &shared.DestinationElasticsearchUpdateAPIKeySecret{
				APIKeyID:     apiKeyID,
				APIKeySecret: apiKeySecret,
			}
		}
		if destinationElasticsearchUpdateAPIKeySecret != nil {
			authenticationMethod = &shared.DestinationElasticsearchUpdateAuthenticationMethod{
				DestinationElasticsearchUpdateAPIKeySecret: destinationElasticsearchUpdateAPIKeySecret,
			}
		}
		var destinationElasticsearchUpdateUsernamePassword *shared.DestinationElasticsearchUpdateUsernamePassword
		if r.Configuration.AuthenticationMethod.UsernamePassword != nil {
			var password string
			password = r.Configuration.AuthenticationMethod.UsernamePassword.Password.ValueString()

			var username string
			username = r.Configuration.AuthenticationMethod.UsernamePassword.Username.ValueString()

			destinationElasticsearchUpdateUsernamePassword = &shared.DestinationElasticsearchUpdateUsernamePassword{
				Password: password,
				Username: username,
			}
		}
		if destinationElasticsearchUpdateUsernamePassword != nil {
			authenticationMethod = &shared.DestinationElasticsearchUpdateAuthenticationMethod{
				DestinationElasticsearchUpdateUsernamePassword: destinationElasticsearchUpdateUsernamePassword,
			}
		}
	}
	caCertificate := new(string)
	if !r.Configuration.CaCertificate.IsUnknown() && !r.Configuration.CaCertificate.IsNull() {
		*caCertificate = r.Configuration.CaCertificate.ValueString()
	} else {
		caCertificate = nil
	}
	var endpoint string
	endpoint = r.Configuration.Endpoint.ValueString()

	pathPrefix := new(string)
	if !r.Configuration.PathPrefix.IsUnknown() && !r.Configuration.PathPrefix.IsNull() {
		*pathPrefix = r.Configuration.PathPrefix.ValueString()
	} else {
		pathPrefix = nil
	}
	var tunnelMethod *shared.DestinationElasticsearchUpdateSSHTunnelMethod
	if r.Configuration.TunnelMethod != nil {
		var destinationElasticsearchUpdateNoTunnel *shared.DestinationElasticsearchUpdateNoTunnel
		if r.Configuration.TunnelMethod.NoTunnel != nil {
			destinationElasticsearchUpdateNoTunnel = &shared.DestinationElasticsearchUpdateNoTunnel{}
		}
		if destinationElasticsearchUpdateNoTunnel != nil {
			tunnelMethod = &shared.DestinationElasticsearchUpdateSSHTunnelMethod{
				DestinationElasticsearchUpdateNoTunnel: destinationElasticsearchUpdateNoTunnel,
			}
		}
		var destinationElasticsearchUpdateSSHKeyAuthentication *shared.DestinationElasticsearchUpdateSSHKeyAuthentication
		if r.Configuration.TunnelMethod.SSHKeyAuthentication != nil {
			var sshKey string
			sshKey = r.Configuration.TunnelMethod.SSHKeyAuthentication.SSHKey.ValueString()

			var tunnelHost string
			tunnelHost = r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelHost.ValueString()

			tunnelPort := new(int64)
			if !r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelPort.IsUnknown() && !r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelPort.IsNull() {
				*tunnelPort = r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelPort.ValueInt64()
			} else {
				tunnelPort = nil
			}
			var tunnelUser string
			tunnelUser = r.Configuration.TunnelMethod.SSHKeyAuthentication.TunnelUser.ValueString()

			destinationElasticsearchUpdateSSHKeyAuthentication = &shared.DestinationElasticsearchUpdateSSHKeyAuthentication{
				SSHKey:     sshKey,
				TunnelHost: tunnelHost,
				TunnelPort: tunnelPort,
				TunnelUser: tunnelUser,
			}
		}
		if destinationElasticsearchUpdateSSHKeyAuthentication != nil {
			tunnelMethod = &shared.DestinationElasticsearchUpdateSSHTunnelMethod{
				DestinationElasticsearchUpdateSSHKeyAuthentication: destinationElasticsearchUpdateSSHKeyAuthentication,
			}
		}
		var destinationElasticsearchUpdatePasswordAuthentication *shared.DestinationElasticsearchUpdatePasswordAuthentication
		if r.Configuration.TunnelMethod.PasswordAuthentication != nil {
			var tunnelHost1 string
			tunnelHost1 = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelHost.ValueString()

			tunnelPort1 := new(int64)
			if !r.Configuration.TunnelMethod.PasswordAuthentication.TunnelPort.IsUnknown() && !r.Configuration.TunnelMethod.PasswordAuthentication.TunnelPort.IsNull() {
				*tunnelPort1 = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelPort.ValueInt64()
			} else {
				tunnelPort1 = nil
			}
			var tunnelUser1 string
			tunnelUser1 = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelUser.ValueString()

			var tunnelUserPassword string
			tunnelUserPassword = r.Configuration.TunnelMethod.PasswordAuthentication.TunnelUserPassword.ValueString()

			destinationElasticsearchUpdatePasswordAuthentication = &shared.DestinationElasticsearchUpdatePasswordAuthentication{
				TunnelHost:         tunnelHost1,
				TunnelPort:         tunnelPort1,
				TunnelUser:         tunnelUser1,
				TunnelUserPassword: tunnelUserPassword,
			}
		}
		if destinationElasticsearchUpdatePasswordAuthentication != nil {
			tunnelMethod = &shared.DestinationElasticsearchUpdateSSHTunnelMethod{
				DestinationElasticsearchUpdatePasswordAuthentication: destinationElasticsearchUpdatePasswordAuthentication,
			}
		}
	}
	upsert := new(bool)
	if !r.Configuration.Upsert.IsUnknown() && !r.Configuration.Upsert.IsNull() {
		*upsert = r.Configuration.Upsert.ValueBool()
	} else {
		upsert = nil
	}
	configuration := shared.DestinationElasticsearchUpdate{
		AuthenticationMethod: authenticationMethod,
		CaCertificate:        caCertificate,
		Endpoint:             endpoint,
		PathPrefix:           pathPrefix,
		TunnelMethod:         tunnelMethod,
		Upsert:               upsert,
	}
	out := shared.DestinationElasticsearchPutRequest{
		Name:          name,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}
