/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{SqlError as e}from"./errorSupport.js";import{isDateOnly as r,isDateTime as t,isTimeOnly as n,isTimestampOffset as i,parseTimestamp as o}from"./sqlDateParsingUtils.js";import{SqlTimeStampOffset as s}from"./SqlTimestampOffset.js";import{isUnknownTimeZone as a}from"./UnknownTimeZone.js";import{isNumber as u,isString as c}from"../../support/guards.js";import{DateTime as f}from"luxon";const l=321408e5,w=26784e5,m=864e5,p=36e5,d=6e4;function h(r){switch(r){case"<>":case"=":return r;case"<":return">";case">":return"<";case"<=":return">=";case">=":return"<=";default:throw new e("UnsupportedOperator",{operator:r})}}function O(e){return!!t(e)||!!i(e)}function v(e){return!!t(e)||(!!r(e)||(!!i(e)||!!n(e)))}function I(n){if(t(n))return n.toMillis();if(r(n))return n.toNumber();if(i(n))return n.toMilliseconds();throw new e("InvalidDataType")}function T(o,s,l){if(null==o||null==s)return null;if(u(o)){if(u(s))return D(o,s,l);if(c(s))return M(o,s,l);if(v(s))throw new e("InvalidOperator");if(r(s))throw new e("InvalidOperator")}else if(c(o)){if(u(s))return y(o,s,l);if(c(s))return z(o,s,l);if(t(s))throw new e("InvalidOperator");if(r(s))throw new e("InvalidOperator");if(n(s))throw new e("InvalidOperator");if(i(s))throw new e("InvalidOperator")}else if(t(o)){if(O(s)){if(a(o.zone)){if(t(s)&&!a(s.zone))return L(o,s,l);if(i(s))return L(o,s,l)}else if(t(s)&&a(s.zone))return L(o,s,l);return D(I(o),I(s),l)}if(c(s))throw new e("InvalidOperator");if(r(s))return j(o,s,l);if(n(s))throw new e("InvalidOperator");if(u(s))throw new e("InvalidOperator")}else if(r(o)){if(i(s))return g(o,s,l);if(t(s))return b(o,s,l);if(c(s))throw new e("InvalidOperator");if(r(s))return N(o,s,l);if(n(s))throw new e("InvalidOperator");if(u(s))throw new e("InvalidOperator")}else if(n(o)){if(n(s))return D(o.toNumber(),s.toNumber(),l);if(c(s))throw new e("InvalidOperator");if(u(s))throw new e("InvalidOperator");if(r(s))throw new e("InvalidOperator");if(O(s))throw new e("InvalidOperator")}else if(i(o)){if(O(s))return f.isDateTime(s)&&a(s.zone)?L(o,s,l):D(I(o),I(s),l);if(c(s))throw new e("InvalidOperator");if(r(s))return x(o,s,l);if(n(s))throw new e("InvalidOperator");if(u(s))throw new e("InvalidOperator")}switch(l){case"<>":return o!==s;case"=":return o===s;case">":return o>s;case"<":return o<s;case">=":return o>=s;case"<=":return o<=s}}function D(e,r,t){switch(t){case"<>":return e!==r;case"=":return e===r;case">":return e>r;case"<":return e<r;case">=":return e>=r;case"<=":return e<=r}}function M(e,r,t){const n=parseFloat(r);if(!isNaN(n))return D(e,n,t);const i=e.toString();switch(t){case"<>":return i!==r;case"=":return i===r;case">":return i>r;case"<":return i<r;case">=":return i>=r;case"<=":return i<=r}}function y(e,r,t){const n=parseFloat(e);if(!isNaN(n))return D(n,r,t);const i=r.toString();switch(t){case"<>":return e!==i;case"=":return e===i;case">":return e>i;case"<":return e<i;case">=":return e>=i;case"<=":return e<=i}}function z(e,r,t){switch(t){case"<>":return e!==r;case"=":return e===r;case">":return e>r;case"<":return e<r;case">=":return e>=r;case"<=":return e<=r}}function N(e,r,t){const n=e.compare(r);switch(t){case"<>":return 0!==n;case"=":return 0===n;case">":return n>0;case"<":return n<0;case">=":return n>=0;case"<=":return n<=0}}function j(e,r,t){const n=r.toDateTimeLuxon(e.zone);return D((e=e.startOf("day")).toMillis(),n.toMillis(),t)}function x(e,r,t){const n=r.toDateTimeLuxon(e.toDateTime().zone);return D((e=e.startOfDay()).toMilliseconds(),n.toMillis(),t)}function b(e,r,t){const n=e.toDateTimeLuxon(r.zone);return r=r.startOf("day"),D(n.toMillis(),r.toMillis(),t)}function g(e,r,t){const n=e.toDateTimeLuxon(r.toDateTime().zone);return r=r.startOfDay(),D(n.toMillis(),r.toMilliseconds(),t)}function L(e,r,t){s.isTimestampOffset(e)&&(e=e.toDateTime()),s.isTimestampOffset(r)&&(r=r.toDateTime());const n=S(e),i=S(r);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 S(e){return e.year*l+e.month*w+e.day*m+e.hour*p+e.minute*d+1e3*e.second+e.millisecond}function U(e,r,t,n){const i=o(n,r);let u;if(s.isTimestampOffset(i))if(a(r)){const e=i.toDateTime();u=f.fromObject({year:e.year,month:e.month,day:e.day,hour:e.hour,minute:e.minute,second:e.second,millisecond:e.millisecond},{zone:r}).toMillis()}else u=i.toMilliseconds();else u=i.toMillis();return{fieldName:e,op:t,comparisonEpochMs:u}}export{I as dateValueInMilliseconds,h as flipSqlComparisonOperator,v as isDateOrTimeValue,O as isDateTimeComparableValue,U as prepareDateFieldToTimestampLiteralComparisonData,T as sqlCompare};
