extends RefCounted
## Skill reset (Boss 2026-09-28): 100,000 Gold gives back every skill point spent on the job's skill
## tree. Server rule (command "skill_reset", server/local_server.gd; network-allowed + economy log in
## net/net_server.gd). Novice skills and free (automatic) skills stay; the reset skills leave the
## hotbar; skill buffs already running keep their time. Asked from the skill window with a confirm.

const COST := 100000


## Skill id -> level that a reset would give back.
static func refundable(ch) -> Dictionary:
	var keep: Array = GameData.jobs.get("novice", {}).get("skills", [])
	var out := {}
	for id in ch.skills:
		var lv := int(ch.skills[id])
		if lv <= 0 or id in keep or bool(GameData.skill(str(id)).get("free", false)):
			continue
		out[str(id)] = lv
	return out


static func points(ch) -> int:
	var n := 0
	var r := refundable(ch)
	for id in r:
		n += int(r[id])
	return n


## "" when the reset can happen, else the reason.
static func check(ch) -> String:
	if points(ch) <= 0:
		return "You have no skill points to reset."
	if int(ch.zeny) < COST:
		return "A skill reset costs 100,000 Gold."
	return ""


static func apply(ch) -> bool:
	var err := check(ch)
	if err != "":
		ch.msg(err, Color(1, 0.5, 0.5))
		return false
	var r := refundable(ch)
	var n := points(ch)
	ch.zeny -= COST
	for id in r:
		ch.skills.erase(id)
		for i in ch.hotbar.size():
			if str(ch.hotbar[i]) == "skill:" + id:
				ch.hotbar[i] = ""
	ch.skill_points += n
	ch.clamp_vitals()
	ch.msg("Your skills were reset: %d skill points returned." % n, Color(1, 0.85, 0.35))
	ch.changed.emit()
	return true
