package convert

import (
	hat_api "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/structs"

	git_model "code.gitea.io/gitea/models/git"
	user_model "code.gitea.io/gitea/models/user"
	"code.gitea.io/gitea/modules/context"
	"code.gitea.io/gitea/modules/git"
	gitea_convert "code.gitea.io/gitea/services/convert"
)

func ToBranch(ctx *context.APIContext, branch *git_model.Branch, c *git.Commit, bp *git_model.ProtectedBranch, user *user_model.User, isRepoAdmin bool) (*hat_api.Branch, error) {
	gitea_api_branch, err := gitea_convert.ToBranch(ctx, ctx.Repo.Repository, branch.Name, c, bp, user, isRepoAdmin)
	if err != nil {
		return nil, err
	}

	hat_api_branch := &hat_api.Branch{}
	hat_api_branch.Branch = gitea_api_branch
	hat_api_branch.IsDeleted = branch.IsDeleted
	hat_api_branch.DeletedUnix = int(branch.DeletedUnix)
	hat_api_branch.ID = branch.ID
	hat_api_branch.DeletedBy = &hat_api.BranchDeleteUser{
		Name:  branch.DeletedBy.Name,
		Email: branch.DeletedBy.Email,
	}
	return hat_api_branch, nil
}
