// Copyright 2022 schukai GmbH
// SPDX-License-Identifier: AGPL-3.0

package configuration

import (
	"gitlab.schukai.com/oss/libraries/go/utilities/pathfinder"
)

// Copy implements the xflags.Copyable interface.
func (s *Settings[C]) Copy(m map[string]any) {

	if s == nil {
		panic("cannot copy nil")
	}

	s.Lock()

	defer func() {
		s.notifyErrorHooks()
	}()

	copyOf := func(s C) C {
		return s
	}

	c := copyOf(s.config)

	for k, v := range m {
		err := pathfinder.SetValue(&c, k, v)
		if err != nil {
			s.errors = append(s.errors, err)
		}
	}

	_, l := s.setConfigInternal(c)
	s.Unlock()

	s.notifyHooks(l)

}