import ae from "./index-typeof2.js";
import B from "./index-createForOfIteratorHelper.js";
import ne from "./index-en-US.js";
import oe from "./index-subMilliseconds.js";
import G from "./index-toDate.js";
import ie from "./index-assign.js";
import J from "./index-longFormatters.js";
import le from "./index-getTimezoneOffsetInMilliseconds.js";
import { isProtectedWeekYearToken as fe, throwProtectedError as U, isProtectedDayOfYearToken as se } from "./index-protectedTokens.js";
import V from "./index-toInteger.js";
import ue from "./index-requiredArgs.js";
import { DateToSystemTimezoneSetter as ve } from "./index-Setter.js";
import { parsers as ce } from "./index-parsers.js";
import { getDefaultOptions as de } from "./index-defaultOptions.js";
var me = /[yYQqMLwIdDecihHKkms]o|(\w)\1*|''|'(''|[^'])+('|$)|./g, pe = /P+p+|P+|p+|''|'(''|[^'])+('|$)|./g, ge = /^'([^]*?)'?$/, he = /''/g, ke = /\S/, we = /[a-zA-Z]/;
function Pe(o, X, q, r) {
  var m, p, g, h, k, w, _, O, y, D, $, S, E, N, T, C, W, b;
  ue(3, arguments);
  var n = String(o), f = String(X), i = de(), s = (m = (p = r == null ? void 0 : r.locale) !== null && p !== void 0 ? p : i.locale) !== null && m !== void 0 ? m : ne;
  if (!s.match)
    throw new RangeError("locale must contain match property");
  var x = V((g = (h = (k = (w = r == null ? void 0 : r.firstWeekContainsDate) !== null && w !== void 0 ? w : r == null || (_ = r.locale) === null || _ === void 0 || (O = _.options) === null || O === void 0 ? void 0 : O.firstWeekContainsDate) !== null && k !== void 0 ? k : i.firstWeekContainsDate) !== null && h !== void 0 ? h : (y = i.locale) === null || y === void 0 || (D = y.options) === null || D === void 0 ? void 0 : D.firstWeekContainsDate) !== null && g !== void 0 ? g : 1);
  if (!(x >= 1 && x <= 7))
    throw new RangeError("firstWeekContainsDate must be between 1 and 7 inclusively");
  var R = V(($ = (S = (E = (N = r == null ? void 0 : r.weekStartsOn) !== null && N !== void 0 ? N : r == null || (T = r.locale) === null || T === void 0 || (C = T.options) === null || C === void 0 ? void 0 : C.weekStartsOn) !== null && E !== void 0 ? E : i.weekStartsOn) !== null && S !== void 0 ? S : (W = i.locale) === null || W === void 0 || (b = W.options) === null || b === void 0 ? void 0 : b.weekStartsOn) !== null && $ !== void 0 ? $ : 0);
  if (!(R >= 0 && R <= 6))
    throw new RangeError("weekStartsOn must be between 0 and 6 inclusively");
  if (f === "")
    return n === "" ? G(q) : /* @__PURE__ */ new Date(NaN);
  var A = {
    firstWeekContainsDate: x,
    weekStartsOn: R,
    locale: s
  }, P = [new ve()], ee = f.match(pe).map(function(t) {
    var e = t[0];
    if (e in J) {
      var a = J[e];
      return a(t, s.formatLong);
    }
    return t;
  }).join("").match(me), F = [], u = B(ee), z;
  try {
    var re = function() {
      var e = z.value;
      !(r != null && r.useAdditionalWeekYearTokens) && fe(e) && U(e, f, o), !(r != null && r.useAdditionalDayOfYearTokens) && se(e) && U(e, f, o);
      var a = e[0], d = ce[a];
      if (d) {
        var Q = d.incompatibleTokens;
        if (Array.isArray(Q)) {
          var K = F.find(function(Z) {
            return Q.includes(Z.token) || Z.token === a;
          });
          if (K)
            throw new RangeError("The format string mustn't contain `".concat(K.fullToken, "` and `").concat(e, "` at the same time"));
        } else if (d.incompatibleTokens === "*" && F.length > 0)
          throw new RangeError("The format string mustn't contain `".concat(e, "` and any other token at the same time"));
        F.push({
          token: a,
          fullToken: e
        });
        var I = d.run(n, e, s.match, A);
        if (!I)
          return {
            v: /* @__PURE__ */ new Date(NaN)
          };
        P.push(I.setter), n = I.rest;
      } else {
        if (a.match(we))
          throw new RangeError("Format string contains an unescaped latin alphabet character `" + a + "`");
        if (e === "''" ? e = "'" : a === "'" && (e = _e(e)), n.indexOf(e) === 0)
          n = n.slice(e.length);
        else
          return {
            v: /* @__PURE__ */ new Date(NaN)
          };
      }
    };
    for (u.s(); !(z = u.n()).done; ) {
      var L = re();
      if (ae(L) === "object") return L.v;
    }
  } catch (t) {
    u.e(t);
  } finally {
    u.f();
  }
  if (n.length > 0 && ke.test(n))
    return /* @__PURE__ */ new Date(NaN);
  var te = P.map(function(t) {
    return t.priority;
  }).sort(function(t, e) {
    return e - t;
  }).filter(function(t, e, a) {
    return a.indexOf(t) === e;
  }).map(function(t) {
    return P.filter(function(e) {
      return e.priority === t;
    }).sort(function(e, a) {
      return a.subPriority - e.subPriority;
    });
  }).map(function(t) {
    return t[0];
  }), Y = G(q);
  if (isNaN(Y.getTime()))
    return /* @__PURE__ */ new Date(NaN);
  var l = oe(Y, le(Y)), M = {}, v = B(te), j;
  try {
    for (v.s(); !(j = v.n()).done; ) {
      var H = j.value;
      if (!H.validate(l, A))
        return /* @__PURE__ */ new Date(NaN);
      var c = H.set(l, M, A);
      Array.isArray(c) ? (l = c[0], ie(M, c[1])) : l = c;
    }
  } catch (t) {
    v.e(t);
  } finally {
    v.f();
  }
  return l;
}
function _e(o) {
  return o.match(ge)[1].replace(he, "'");
}
export {
  Pe as default
};
