/* © SRSoftware 2025 */
package de.srsoftware.umbrella.core.model;

import static de.srsoftware.tools.Optionals.isSet;

import de.srsoftware.tools.Mappable;
import de.srsoftware.umbrella.core.constants.Field;
import java.util.HashMap;
import java.util.Map;

public class IdOrString implements Mappable {
		private final Long id;
		private final String value;

		public IdOrString(String val){
			this.value = val;
			this.id = parseOrNull(val);
		}

		public IdOrString(long id){
			this.value = ""+id;
			this.id = id;
		}

		public boolean isId(){
			return id != null;
		}

		public static IdOrString of(String val){
			return new IdOrString(val);
		}

		public static IdOrString of(UmbrellaUser user) {
			return new IdOrString(user.id());
		}

		private static Long parseOrNull(String val) {
			try {
				return Long.parseLong(val);
			} catch (NumberFormatException e) {
				return null;
			}
		}

		public long id(){
			return id;
		}

		@Override
		public Map<String, Object> toMap() {
			var map = new HashMap<String,Object>();
			map.put(Field.VALUE,value);
			if (isId()) map.put(Field.ID, id);
			return map;
		}

	public boolean isEmpty() {
			return id == null && !isSet(value);
	}

	@Override
	public String toString() {
		return value;
	}

	public String value(){
			return value;
		}
}