// Code generated by eevee. DO NOT EDIT!

package repository

import (
	"/dao"
	"/entity"
	"/model"
	"context"
	"database/sql"

	"golang.org/x/xerrors"
)

type UserField interface {
	ToModel(*entity.UserField) *model.UserField
	ToModels(entity.UserFields) *model.UserFields
	Create(context.Context, *entity.UserField) (*model.UserField, error)
	Creates(context.Context, entity.UserFields) (*model.UserFields, error)
}

type UserFieldImpl struct {
	userFieldDAO dao.UserField
	repo         Repository
}

func NewUserField(ctx context.Context, tx *sql.Tx) *UserFieldImpl {
	return &UserFieldImpl{userFieldDAO: dao.NewUserField(ctx, tx)}
}

func (r *UserFieldImpl) ToModel(value *entity.UserField) *model.UserField {
	return r.createCollection(entity.UserFields{value}).First()
}

func (r *UserFieldImpl) ToModels(values entity.UserFields) *model.UserFields {
	return r.createCollection(values)
}

func (r *UserFieldImpl) Create(ctx context.Context, value *entity.UserField) (*model.UserField, error) {
	if err := r.userFieldDAO.Create(ctx, value); err != nil {
		return nil, xerrors.Errorf("cannot Create: %w", err)
	}
	v := r.ToModel(value)
	v.SetSavedValue(value)
	v.SetAlreadyCreated(true)
	return v, nil
}

func (r *UserFieldImpl) Creates(ctx context.Context, entities entity.UserFields) (*model.UserFields, error) {
	for _, v := range entities {
		if _, err := r.Create(ctx, v); err != nil {
			return nil, xerrors.Errorf("cannot Create: %w", err)
		}
	}
	values := r.ToModels(entities)
	values.Each(func(v *model.UserField) {
		v.SetSavedValue(v.UserField)
		v.SetAlreadyCreated(true)
	})
	return values, nil
}

func (r *UserFieldImpl) createCollection(entities entity.UserFields) *model.UserFields {
	values := model.NewUserFields(entities)
	for i := 0; i < len(entities); i += 1 {
		values.Add(r.create(entities[i], values))
	}
	return values
}

func (r *UserFieldImpl) create(entity *entity.UserField, values *model.UserFields) *model.UserField {
	value := model.NewUserField(entity, r.userFieldDAO)
	value.SetConverter(r.repo.(model.ModelConverter))
	return value
}
