import { MIN_LENGTH_FOR_NSN as h, MAX_LENGTH_FOR_NSN as T, PLUS_CHARS as d, VALID_DIGITS as g } from "./index-constants3.js";
import m from "./index-ParseError.js";
import O, { getCountryCallingCode as P } from "./index-metadata.js";
import v, { isViablePhoneNumberStart as s } from "./index-isViablePhoneNumber.js";
import w from "./index-extractExtension.js";
import y from "./index-parseIncompletePhoneNumber.js";
import { isPossibleNumber as S } from "./index-isPossible.js";
import x from "./index-PhoneNumber2.js";
import R from "./index-matchesEntirely.js";
import E from "./index-extractCountryCallingCode.js";
import I from "./index-extractNationalNumber.js";
import A from "./index-getCountryByCallingCode.js";
import p from "./index-extractFormattedPhoneNumberFromPossibleRfc3966NumberUri.js";
var H = 250, L = new RegExp("[" + d + g + "]"), U = new RegExp("[^" + g + "#]+$");
function Z(t, r, n) {
  if (r = r || {}, n = new O(n), r.defaultCountry && !n.hasCountry(r.defaultCountry))
    throw r.v2 ? new m("INVALID_COUNTRY") : new Error("Unknown country: ".concat(r.defaultCountry));
  var e = G(t, r.v2, r.extract), l = e.number, a = e.ext, u = e.error;
  if (!l) {
    if (r.v2)
      throw u === "TOO_SHORT" ? new m("TOO_SHORT") : new m("NOT_A_NUMBER");
    return {};
  }
  var c = D(l, r.defaultCountry, r.defaultCallingCode, n), i = c.country, o = c.nationalNumber, N = c.countryCallingCode, _ = c.countryCallingCodeSource, f = c.carrierCode;
  if (!n.hasSelectedNumberingPlan()) {
    if (r.v2)
      throw new m("INVALID_COUNTRY");
    return {};
  }
  if (!o || o.length < h) {
    if (r.v2)
      throw new m("TOO_SHORT");
    return {};
  }
  if (o.length > T) {
    if (r.v2)
      throw new m("TOO_LONG");
    return {};
  }
  if (r.v2) {
    var C = new x(N, o, n.metadata);
    return i && (C.country = i), f && (C.carrierCode = f), a && (C.ext = a), C.__countryCallingCodeSource = _, C;
  }
  var b = (r.extended ? n.hasSelectedNumberingPlan() : i) ? R(o, n.nationalNumberPattern()) : !1;
  return r.extended ? {
    country: i,
    countryCallingCode: N,
    carrierCode: f,
    valid: b,
    possible: b ? !0 : !!(r.extended === !0 && n.possibleLengths() && S(o, n)),
    phone: o,
    ext: a
  } : b ? M(i, o, a) : {};
}
function F(t, r, n) {
  if (t) {
    if (t.length > H) {
      if (n)
        throw new m("TOO_LONG");
      return;
    }
    if (r === !1)
      return t;
    var e = t.search(L);
    if (!(e < 0))
      return t.slice(e).replace(U, "");
  }
}
function G(t, r, n) {
  var e = p(t, {
    extractFormattedPhoneNumber: function(u) {
      return F(u, n, r);
    }
  });
  if (!e)
    return {};
  if (!v(e))
    return s(e) ? {
      error: "TOO_SHORT"
    } : {};
  var l = w(e);
  return l.ext ? l : {
    number: e
  };
}
function M(t, r, n) {
  var e = {
    country: t,
    phone: r
  };
  return n && (e.ext = n), e;
}
function D(t, r, n, e) {
  var l = E(y(t), r, n, e.metadata), a = l.countryCallingCodeSource, u = l.countryCallingCode, c = l.number, i;
  if (u)
    e.selectNumberingPlan(u);
  else if (c && (r || n))
    e.selectNumberingPlan(r, n), r && (i = r), u = n || P(r, e.metadata);
  else return {};
  if (!c)
    return {
      countryCallingCodeSource: a,
      countryCallingCode: u
    };
  var o = I(y(c), e), N = o.nationalNumber, _ = o.carrierCode, f = A(u, N, e);
  return f && (i = f, f === "001" || e.country(i)), {
    country: i,
    countryCallingCode: u,
    countryCallingCodeSource: a,
    nationalNumber: N,
    carrierCode: _
  };
}
export {
  Z as default
};
