/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{SqlError as r}from"./errorSupport.js";import{isDateOnly as e,isDateTime as t,isTimeOnly as n,isTimestampOffset as i}from"./sqlDateParsingUtils.js";import{SqlTimeStampOffset as o}from"./SqlTimestampOffset.js";import{isUnknownTimeZone as a}from"./UnknownTimeZone.js";import{isNumber as s,isString as u}from"../../support/guards.js";import{DateTime as f}from"luxon";const c=321408e5,l=26784e5,w=864e5,m=36e5,p=6e4;function d(r){return!!t(r)||!!i(r)}function h(r){return!!t(r)||(!!e(r)||(!!i(r)||!!n(r)))}function O(n){if(t(n))return n.toMillis();if(e(n))return n.toNumber();if(i(n))return n.toMilliseconds();throw new r("InvalidDataType")}function v(c,l,w){if(null==c||null==l)return null;if(s(c)){if(s(l))return I(c,l,w);if(u(l))return T(c,l,w);if(h(l))throw new r("InvalidOperator");if(e(l))throw new r("InvalidOperator")}else if(u(c)){if(s(l))return D(c,l,w);if(u(l))return M(c,l,w);if(t(l))throw new r("InvalidOperator");if(e(l))throw new r("InvalidOperator");if(n(l))throw new r("InvalidOperator");if(i(l))throw new r("InvalidOperator")}else if(t(c)){if(d(l)){if(a(c.zone)){if(f.isDateTime(l)&&!a(l.zone))return b(c,l,w);if(o.isTimestampOffset(l))return b(c,l,w)}else if(f.isDateTime(l)&&a(l.zone)){if(!a(c.zone))return b(c,l,w);if(o.isTimestampOffset(c))return b(c,l,w)}return I(O(c),O(l),w)}if(u(l))throw new r("InvalidOperator");if(e(l))return z(c,l,w);if(n(l))throw new r("InvalidOperator");if(s(l))throw new r("InvalidOperator")}else if(e(c)){if(i(l))return x(c,l,w);if(t(l))return y(c,l,w);if(u(l))throw new r("InvalidOperator");if(e(l))return I(c.toNumber(),l.toNumber(),w);if(n(l))throw new r("InvalidOperator");if(s(l))throw new r("InvalidOperator")}else if(n(c)){if(n(l))return I(c.toNumber(),l.toNumber(),w);if(u(l))throw new r("InvalidOperator");if(s(l))throw new r("InvalidOperator");if(e(l))throw new r("InvalidOperator");if(d(l))throw new r("InvalidOperator")}else if(i(c)){if(d(l))return f.isDateTime(l)&&a(l.zone)?b(c,l,w):I(O(c),O(l),w);if(u(l))throw new r("InvalidOperator");if(e(l))return N(c,l,w);if(n(l))throw new r("InvalidOperator");if(s(l))throw new r("InvalidOperator")}switch(w){case"<>":return c!==l;case"=":return c===l;case">":return c>l;case"<":return c<l;case">=":return c>=l;case"<=":return c<=l}}function I(r,e,t){switch(t){case"<>":return r!==e;case"=":return r===e;case">":return r>e;case"<":return r<e;case">=":return r>=e;case"<=":return r<=e}}function T(r,e,t){const n=parseFloat(e);if(!isNaN(n))return I(r,n,t);const i=r.toString();switch(t){case"<>":return i!==e;case"=":return i===e;case">":return i>e;case"<":return i<e;case">=":return i>=e;case"<=":return i<=e}}function D(r,e,t){const n=parseFloat(r);if(!isNaN(n))return I(n,e,t);const i=e.toString();switch(t){case"<>":return r!==i;case"=":return r===i;case">":return r>i;case"<":return r<i;case">=":return r>=i;case"<=":return r<=i}}function M(r,e,t){switch(t){case"<>":return r!==e;case"=":return r===e;case">":return r>e;case"<":return r<e;case">=":return r>=e;case"<=":return r<=e}}function z(r,e,t){const n=e.toDateTimeLuxon(r.zone);return I((r=r.startOf("day")).toMillis(),n.toMillis(),t)}function N(r,e,t){const n=e.toDateTimeLuxon(r.toDateTime().zone);return I((r=r.startOfDay()).toMilliseconds(),n.toMillis(),t)}function y(r,e,t){const n=r.toDateTimeLuxon(e.zone);return e=e.startOf("day"),I(n.toMillis(),e.toMillis(),t)}function x(r,e,t){const n=r.toDateTimeLuxon(e.toDateTime().zone);return e=e.startOfDay(),I(n.toMillis(),e.toMilliseconds(),t)}function b(r,e,t){o.isTimestampOffset(r)&&(r=r.toDateTime()),o.isTimestampOffset(e)&&(e=e.toDateTime());const n=j(r),i=j(e);switch(t){case"<>":return n!==i;case"=":return n===i;case">":return n>i;case"<":return n<i;case">=":return n>=i;case"<=":return n<=i}}function j(r){return r.year*c+r.month*l+r.day*w+r.hour*m+r.minute*p+1e3*r.second+r.millisecond}export{O as dateValueInMilliseconds,h as isDateOrTimeValue,d as isDateTimeComparableValue,v as sqlCompare};
