package lucee.runtime.listener;

import lucee.commons.lang.StringUtil;
import lucee.runtime.exp.ApplicationException;
import lucee.runtime.type.dt.TimeSpan;
import lucee.runtime.type.dt.TimeSpanImpl;
import lucee.runtime.type.scope.CookieImpl;

public class SessionCookieDataImpl implements SessionCookieData {

	public static final SessionCookieData DEFAULT = new SessionCookieDataImpl(true, false, TimeSpanImpl.fromMillis(CookieImpl.NEVER * 1000), null, false, CookieData.SAMESITE_NONE);

	private final boolean httpOnly;
	private final boolean secure;
	private final TimeSpan timeout;
	private final String domain;
	private final boolean disableUpdate;
	private final short samesite;

	public SessionCookieDataImpl(boolean httpOnly, boolean secure, TimeSpan timeout, String domain, boolean disableUpdate, short samesite) {
		this.httpOnly = httpOnly;
		this.secure = secure;
		this.timeout = timeout;
		this.domain = StringUtil.isEmpty(domain, true) ? null : domain.trim();
		this.disableUpdate = disableUpdate;
		this.samesite = samesite;
	}

	@Override
	public boolean isHttpOnly() {
		return httpOnly;
	}

	@Override
	public boolean isSecure() {
		return secure;
	}

	@Override
	public TimeSpan getTimeout() {
		return timeout;
	}

	@Override
	public String getDomain() {
		return domain;
	}

	@Override
	public boolean isDisableUpdate() {
		return disableUpdate;
	}

	@Override
	public short getSamesite() {
		return samesite;
	}

	public static short toSamesite(String str) throws ApplicationException {
		if (StringUtil.isEmpty(str)) return SAMESITE_NONE;
		str = str.trim();
		if ("NONE".equalsIgnoreCase(str)) return SAMESITE_NONE;
		if ("LAX".equalsIgnoreCase(str)) return SAMESITE_LAX;
		if ("STRICT".equalsIgnoreCase(str)) return SAMESITE_STRICT;
		throw new ApplicationException("invalid value [" + str + "] for samesite, valid values are [none,lax,strict]");
	}

	public static short toSamesite(String str, short defaultValue) {
		if (StringUtil.isEmpty(str)) return SAMESITE_NONE;
		str = str.trim();
		if ("NONE".equalsIgnoreCase(str)) return SAMESITE_NONE;
		if ("LAX".equalsIgnoreCase(str)) return SAMESITE_LAX;
		if ("STRICT".equalsIgnoreCase(str)) return SAMESITE_STRICT;
		return defaultValue;
	}

	public static String toSamesite(short s) {
		if (s == SAMESITE_STRICT) return "Strict";
		if (s == SAMESITE_LAX) return "Lax";
		return "None";
	}
}

@@ -611,16 +611,44 @@
 domain;%0A
+%09private final String path;%0A
 %09private
@@ -843,16 +843,29 @@
 samesite
+, String path
 ) %7B%0A%09%09th
@@ -1011,16 +1011,83 @@
 trim();%0A
+%09%09this.path = StringUtil.isEmpty(path, true) ? null : path.trim();%0A
 %09%09this.d
@@ -1394,24 +1394,82 @@
 n domain;%0A%09%7D
+%0A%09%0A%09@Override%0A%09public String getPath() %7B%0A%09%09return path;%0A%09%7D
 %0A%0A%09@Override

add path to session cookie