// Code generated by azure-service-operator-codegen. DO NOT EDIT.
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
package compat

import (
	storage "github.com/Azure/azure-service-operator/v2/api/apimanagement/v1api20240501/storage"
	"github.com/Azure/azure-service-operator/v2/pkg/genruntime"
	"github.com/rotisserie/eris"
)

// Storage version of v1api20230501preview.BackendPool_STATUS
// Backend pool information
type BackendPool_STATUS struct {
	PropertyBag genruntime.PropertyBag   `json:"$propertyBag,omitempty"`
	Services    []BackendPoolItem_STATUS `json:"services,omitempty"`
}

// AssignProperties_From_BackendPool_STATUS populates our BackendPool_STATUS from the provided source BackendPool_STATUS
func (pool *BackendPool_STATUS) AssignProperties_From_BackendPool_STATUS(source *storage.BackendPool_STATUS) error {
	// Clone the existing property bag
	propertyBag := genruntime.NewPropertyBag(source.PropertyBag)

	// Services
	if source.Services != nil {
		serviceList := make([]BackendPoolItem_STATUS, len(source.Services))
		for serviceIndex, serviceItem := range source.Services {
			var service BackendPoolItem_STATUS
			err := service.AssignProperties_From_BackendPoolItem_STATUS(&serviceItem)
			if err != nil {
				return eris.Wrap(err, "calling AssignProperties_From_BackendPoolItem_STATUS() to populate field Services")
			}
			serviceList[serviceIndex] = service
		}
		pool.Services = serviceList
	} else {
		pool.Services = nil
	}

	// Update the property bag
	if len(propertyBag) > 0 {
		pool.PropertyBag = propertyBag
	} else {
		pool.PropertyBag = nil
	}

	// Invoke the augmentConversionForBackendPool_STATUS interface (if implemented) to customize the conversion
	var poolAsAny any = pool
	if augmentedPool, ok := poolAsAny.(augmentConversionForBackendPool_STATUS); ok {
		err := augmentedPool.AssignPropertiesFrom(source)
		if err != nil {
			return eris.Wrap(err, "calling augmented AssignPropertiesFrom() for conversion")
		}
	}

	// No error
	return nil
}

// AssignProperties_To_BackendPool_STATUS populates the provided destination BackendPool_STATUS from our BackendPool_STATUS
func (pool *BackendPool_STATUS) AssignProperties_To_BackendPool_STATUS(destination *storage.BackendPool_STATUS) error {
	// Clone the existing property bag
	propertyBag := genruntime.NewPropertyBag(pool.PropertyBag)

	// Services
	if pool.Services != nil {
		serviceList := make([]storage.BackendPoolItem_STATUS, len(pool.Services))
		for serviceIndex, serviceItem := range pool.Services {
			var service storage.BackendPoolItem_STATUS
			err := serviceItem.AssignProperties_To_BackendPoolItem_STATUS(&service)
			if err != nil {
				return eris.Wrap(err, "calling AssignProperties_To_BackendPoolItem_STATUS() to populate field Services")
			}
			serviceList[serviceIndex] = service
		}
		destination.Services = serviceList
	} else {
		destination.Services = nil
	}

	// Update the property bag
	if len(propertyBag) > 0 {
		destination.PropertyBag = propertyBag
	} else {
		destination.PropertyBag = nil
	}

	// Invoke the augmentConversionForBackendPool_STATUS interface (if implemented) to customize the conversion
	var poolAsAny any = pool
	if augmentedPool, ok := poolAsAny.(augmentConversionForBackendPool_STATUS); ok {
		err := augmentedPool.AssignPropertiesTo(destination)
		if err != nil {
			return eris.Wrap(err, "calling augmented AssignPropertiesTo() for conversion")
		}
	}

	// No error
	return nil
}

type augmentConversionForBackendPool_STATUS interface {
	AssignPropertiesFrom(src *storage.BackendPool_STATUS) error
	AssignPropertiesTo(dst *storage.BackendPool_STATUS) error
}

// Storage version of v1api20230501preview.BackendPoolItem_STATUS
// Backend pool service information
type BackendPoolItem_STATUS struct {
	Id          *string                `json:"id,omitempty"`
	PropertyBag genruntime.PropertyBag `json:"$propertyBag,omitempty"`
}

// AssignProperties_From_BackendPoolItem_STATUS populates our BackendPoolItem_STATUS from the provided source BackendPoolItem_STATUS
func (item *BackendPoolItem_STATUS) AssignProperties_From_BackendPoolItem_STATUS(source *storage.BackendPoolItem_STATUS) error {
	// Clone the existing property bag
	propertyBag := genruntime.NewPropertyBag(source.PropertyBag)

	// Id
	item.Id = genruntime.ClonePointerToString(source.Id)

	// Priority
	if source.Priority != nil {
		propertyBag.Add("Priority", *source.Priority)
	} else {
		propertyBag.Remove("Priority")
	}

	// Weight
	if source.Weight != nil {
		propertyBag.Add("Weight", *source.Weight)
	} else {
		propertyBag.Remove("Weight")
	}

	// Update the property bag
	if len(propertyBag) > 0 {
		item.PropertyBag = propertyBag
	} else {
		item.PropertyBag = nil
	}

	// Invoke the augmentConversionForBackendPoolItem_STATUS interface (if implemented) to customize the conversion
	var itemAsAny any = item
	if augmentedItem, ok := itemAsAny.(augmentConversionForBackendPoolItem_STATUS); ok {
		err := augmentedItem.AssignPropertiesFrom(source)
		if err != nil {
			return eris.Wrap(err, "calling augmented AssignPropertiesFrom() for conversion")
		}
	}

	// No error
	return nil
}

// AssignProperties_To_BackendPoolItem_STATUS populates the provided destination BackendPoolItem_STATUS from our BackendPoolItem_STATUS
func (item *BackendPoolItem_STATUS) AssignProperties_To_BackendPoolItem_STATUS(destination *storage.BackendPoolItem_STATUS) error {
	// Clone the existing property bag
	propertyBag := genruntime.NewPropertyBag(item.PropertyBag)

	// Id
	destination.Id = genruntime.ClonePointerToString(item.Id)

	// Priority
	if propertyBag.Contains("Priority") {
		var priority int
		err := propertyBag.Pull("Priority", &priority)
		if err != nil {
			return eris.Wrap(err, "pulling 'Priority' from propertyBag")
		}

		destination.Priority = &priority
	} else {
		destination.Priority = nil
	}

	// Weight
	if propertyBag.Contains("Weight") {
		var weight int
		err := propertyBag.Pull("Weight", &weight)
		if err != nil {
			return eris.Wrap(err, "pulling 'Weight' from propertyBag")
		}

		destination.Weight = &weight
	} else {
		destination.Weight = nil
	}

	// Update the property bag
	if len(propertyBag) > 0 {
		destination.PropertyBag = propertyBag
	} else {
		destination.PropertyBag = nil
	}

	// Invoke the augmentConversionForBackendPoolItem_STATUS interface (if implemented) to customize the conversion
	var itemAsAny any = item
	if augmentedItem, ok := itemAsAny.(augmentConversionForBackendPoolItem_STATUS); ok {
		err := augmentedItem.AssignPropertiesTo(destination)
		if err != nil {
			return eris.Wrap(err, "calling augmented AssignPropertiesTo() for conversion")
		}
	}

	// No error
	return nil
}

type augmentConversionForBackendPoolItem_STATUS interface {
	AssignPropertiesFrom(src *storage.BackendPoolItem_STATUS) error
	AssignPropertiesTo(dst *storage.BackendPoolItem_STATUS) error
}
