// 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 *SourceNewsAPIResourceModel) ToSharedSourceNewsAPICreateRequest() *shared.SourceNewsAPICreateRequest {
	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 apiKey string
	apiKey = r.Configuration.APIKey.ValueString()

	category := new(shared.Category)
	if !r.Configuration.Category.IsUnknown() && !r.Configuration.Category.IsNull() {
		*category = shared.Category(r.Configuration.Category.ValueString())
	} else {
		category = nil
	}
	country := new(shared.Country)
	if !r.Configuration.Country.IsUnknown() && !r.Configuration.Country.IsNull() {
		*country = shared.Country(r.Configuration.Country.ValueString())
	} else {
		country = nil
	}
	var domains []string = []string{}
	for _, domainsItem := range r.Configuration.Domains {
		domains = append(domains, domainsItem.ValueString())
	}
	endDate := new(string)
	if !r.Configuration.EndDate.IsUnknown() && !r.Configuration.EndDate.IsNull() {
		*endDate = r.Configuration.EndDate.ValueString()
	} else {
		endDate = nil
	}
	var excludeDomains []string = []string{}
	for _, excludeDomainsItem := range r.Configuration.ExcludeDomains {
		excludeDomains = append(excludeDomains, excludeDomainsItem.ValueString())
	}
	language := new(shared.Language)
	if !r.Configuration.Language.IsUnknown() && !r.Configuration.Language.IsNull() {
		*language = shared.Language(r.Configuration.Language.ValueString())
	} else {
		language = nil
	}
	var searchIn []shared.SearchIn = []shared.SearchIn{}
	for _, searchInItem := range r.Configuration.SearchIn {
		searchIn = append(searchIn, shared.SearchIn(searchInItem.ValueString()))
	}
	searchQuery := new(string)
	if !r.Configuration.SearchQuery.IsUnknown() && !r.Configuration.SearchQuery.IsNull() {
		*searchQuery = r.Configuration.SearchQuery.ValueString()
	} else {
		searchQuery = nil
	}
	sortBy := new(shared.SortBy)
	if !r.Configuration.SortBy.IsUnknown() && !r.Configuration.SortBy.IsNull() {
		*sortBy = shared.SortBy(r.Configuration.SortBy.ValueString())
	} else {
		sortBy = nil
	}
	var sources []string = []string{}
	for _, sourcesItem := range r.Configuration.Sources {
		sources = append(sources, sourcesItem.ValueString())
	}
	startDate := new(string)
	if !r.Configuration.StartDate.IsUnknown() && !r.Configuration.StartDate.IsNull() {
		*startDate = r.Configuration.StartDate.ValueString()
	} else {
		startDate = nil
	}
	configuration := shared.SourceNewsAPI{
		APIKey:         apiKey,
		Category:       category,
		Country:        country,
		Domains:        domains,
		EndDate:        endDate,
		ExcludeDomains: excludeDomains,
		Language:       language,
		SearchIn:       searchIn,
		SearchQuery:    searchQuery,
		SortBy:         sortBy,
		Sources:        sources,
		StartDate:      startDate,
	}
	secretID := new(string)
	if !r.SecretID.IsUnknown() && !r.SecretID.IsNull() {
		*secretID = r.SecretID.ValueString()
	} else {
		secretID = nil
	}
	out := shared.SourceNewsAPICreateRequest{
		Name:          name,
		DefinitionID:  definitionID,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
		SecretID:      secretID,
	}
	return &out
}

func (r *SourceNewsAPIResourceModel) RefreshFromSharedSourceResponse(resp *shared.SourceResponse) {
	if resp != nil {
		r.CreatedAt = types.Int64Value(resp.CreatedAt)
		r.DefinitionID = types.StringValue(resp.DefinitionID)
		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.SourceID = types.StringValue(resp.SourceID)
		r.SourceType = types.StringValue(resp.SourceType)
		r.WorkspaceID = types.StringValue(resp.WorkspaceID)
	}
}

func (r *SourceNewsAPIResourceModel) ToSharedSourceNewsAPIPutRequest() *shared.SourceNewsAPIPutRequest {
	var name string
	name = r.Name.ValueString()

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

	var apiKey string
	apiKey = r.Configuration.APIKey.ValueString()

	category := new(shared.SourceNewsAPIUpdateCategory)
	if !r.Configuration.Category.IsUnknown() && !r.Configuration.Category.IsNull() {
		*category = shared.SourceNewsAPIUpdateCategory(r.Configuration.Category.ValueString())
	} else {
		category = nil
	}
	country := new(shared.SourceNewsAPIUpdateCountry)
	if !r.Configuration.Country.IsUnknown() && !r.Configuration.Country.IsNull() {
		*country = shared.SourceNewsAPIUpdateCountry(r.Configuration.Country.ValueString())
	} else {
		country = nil
	}
	var domains []string = []string{}
	for _, domainsItem := range r.Configuration.Domains {
		domains = append(domains, domainsItem.ValueString())
	}
	endDate := new(string)
	if !r.Configuration.EndDate.IsUnknown() && !r.Configuration.EndDate.IsNull() {
		*endDate = r.Configuration.EndDate.ValueString()
	} else {
		endDate = nil
	}
	var excludeDomains []string = []string{}
	for _, excludeDomainsItem := range r.Configuration.ExcludeDomains {
		excludeDomains = append(excludeDomains, excludeDomainsItem.ValueString())
	}
	language := new(shared.SourceNewsAPIUpdateLanguage)
	if !r.Configuration.Language.IsUnknown() && !r.Configuration.Language.IsNull() {
		*language = shared.SourceNewsAPIUpdateLanguage(r.Configuration.Language.ValueString())
	} else {
		language = nil
	}
	var searchIn []shared.SourceNewsAPIUpdateSearchIn = []shared.SourceNewsAPIUpdateSearchIn{}
	for _, searchInItem := range r.Configuration.SearchIn {
		searchIn = append(searchIn, shared.SourceNewsAPIUpdateSearchIn(searchInItem.ValueString()))
	}
	searchQuery := new(string)
	if !r.Configuration.SearchQuery.IsUnknown() && !r.Configuration.SearchQuery.IsNull() {
		*searchQuery = r.Configuration.SearchQuery.ValueString()
	} else {
		searchQuery = nil
	}
	sortBy := new(shared.SourceNewsAPIUpdateSortBy)
	if !r.Configuration.SortBy.IsUnknown() && !r.Configuration.SortBy.IsNull() {
		*sortBy = shared.SourceNewsAPIUpdateSortBy(r.Configuration.SortBy.ValueString())
	} else {
		sortBy = nil
	}
	var sources []string = []string{}
	for _, sourcesItem := range r.Configuration.Sources {
		sources = append(sources, sourcesItem.ValueString())
	}
	startDate := new(string)
	if !r.Configuration.StartDate.IsUnknown() && !r.Configuration.StartDate.IsNull() {
		*startDate = r.Configuration.StartDate.ValueString()
	} else {
		startDate = nil
	}
	configuration := shared.SourceNewsAPIUpdate{
		APIKey:         apiKey,
		Category:       category,
		Country:        country,
		Domains:        domains,
		EndDate:        endDate,
		ExcludeDomains: excludeDomains,
		Language:       language,
		SearchIn:       searchIn,
		SearchQuery:    searchQuery,
		SortBy:         sortBy,
		Sources:        sources,
		StartDate:      startDate,
	}
	out := shared.SourceNewsAPIPutRequest{
		Name:          name,
		WorkspaceID:   workspaceID,
		Configuration: configuration,
	}
	return &out
}
