import I from "./index-isValid2.js";
import y from "./index-subMilliseconds.js";
import M from "./index-toDate.js";
import j from "./index-formatters.js";
import q from "./index-longFormatters.js";
import Q from "./index-getTimezoneOffsetInMilliseconds.js";
import { isProtectedWeekYearToken as F, throwProtectedError as T, isProtectedDayOfYearToken as H } from "./index-protectedTokens.js";
import x from "./index-toInteger.js";
import K from "./index-requiredArgs.js";
import { getDefaultOptions as V } from "./index-defaultOptions.js";
import Z from "./index-en-US.js";
var B = /[yYQqMLwIdDecihHKkms]o|(\w)\1*|''|'(''|[^'])+('|$)|./g, G = /P+p+|P+|p+|''|'(''|[^'])+('|$)|./g, J = /^'([^]*?)'?$/, N = /''/g, U = /[a-zA-Z]/;
function de(a, t, e) {
  var v, d, c, u, m, s, p, k, w, g, O, _, h, $, D, E, S, C;
  K(2, arguments);
  var z = String(t), l = V(), n = (v = (d = e == null ? void 0 : e.locale) !== null && d !== void 0 ? d : l.locale) !== null && v !== void 0 ? v : Z, R = x((c = (u = (m = (s = e == null ? void 0 : e.firstWeekContainsDate) !== null && s !== void 0 ? s : e == null || (p = e.locale) === null || p === void 0 || (k = p.options) === null || k === void 0 ? void 0 : k.firstWeekContainsDate) !== null && m !== void 0 ? m : l.firstWeekContainsDate) !== null && u !== void 0 ? u : (w = l.locale) === null || w === void 0 || (g = w.options) === null || g === void 0 ? void 0 : g.firstWeekContainsDate) !== null && c !== void 0 ? c : 1);
  if (!(R >= 1 && R <= 7))
    throw new RangeError("firstWeekContainsDate must be between 1 and 7 inclusively");
  var W = x((O = (_ = (h = ($ = e == null ? void 0 : e.weekStartsOn) !== null && $ !== void 0 ? $ : e == null || (D = e.locale) === null || D === void 0 || (E = D.options) === null || E === void 0 ? void 0 : E.weekStartsOn) !== null && h !== void 0 ? h : l.weekStartsOn) !== null && _ !== void 0 ? _ : (S = l.locale) === null || S === void 0 || (C = S.options) === null || C === void 0 ? void 0 : C.weekStartsOn) !== null && O !== void 0 ? O : 0);
  if (!(W >= 0 && W <= 6))
    throw new RangeError("weekStartsOn must be between 0 and 6 inclusively");
  if (!n.localize)
    throw new RangeError("locale must contain localize property");
  if (!n.formatLong)
    throw new RangeError("locale must contain formatLong property");
  var i = M(a);
  if (!I(i))
    throw new RangeError("Invalid time value");
  var P = Q(i), L = y(i, P), Y = {
    firstWeekContainsDate: R,
    weekStartsOn: W,
    locale: n,
    _originalDate: i
  }, A = z.match(G).map(function(r) {
    var o = r[0];
    if (o === "p" || o === "P") {
      var f = q[o];
      return f(r, n.formatLong);
    }
    return r;
  }).join("").match(B).map(function(r) {
    if (r === "''")
      return "'";
    var o = r[0];
    if (o === "'")
      return X(r);
    var f = j[o];
    if (f)
      return !(e != null && e.useAdditionalWeekYearTokens) && F(r) && T(r, t, String(a)), !(e != null && e.useAdditionalDayOfYearTokens) && H(r) && T(r, t, String(a)), f(L, r, n.localize, Y);
    if (o.match(U))
      throw new RangeError("Format string contains an unescaped latin alphabet character `" + o + "`");
    return r;
  }).join("");
  return A;
}
function X(a) {
  var t = a.match(J);
  return t ? t[1].replace(N, "'") : a;
}
export {
  de as default
};
