package client

import (
	"context"
	"errors"

	"coopcloud.tech/abra/pkg/i18n"
	"github.com/docker/docker/api/types/filters"
	"github.com/docker/docker/api/types/swarm"
	"github.com/docker/docker/client"
)

func GetConfigs(cl *client.Client, ctx context.Context, server string, fs filters.Args) ([]swarm.Config, error) {
	configList, err := cl.ConfigList(ctx, swarm.ConfigListOptions{Filters: fs})
	if err != nil {
		return configList, err
	}

	return configList, nil
}

func GetConfigNames(configs []swarm.Config) []string {
	var confNames []string

	for _, conf := range configs {
		confNames = append(confNames, conf.Spec.Name)
	}

	return confNames
}

func RemoveConfigs(cl *client.Client, ctx context.Context, configNames []string, force bool) error {
	for _, confName := range configNames {
		if err := cl.ConfigRemove(context.Background(), confName); err != nil {
			return errors.New(i18n.G("conf %s: %s", confName, err))
		}
	}
	return nil
}
