import g from "./index-matchesEntirely.js";
import O from "./index-formatNationalNumberUsingFormat.js";
import b, { getCountryCallingCode as N } from "./index-metadata.js";
import v from "./index-getIddPrefix.js";
import { formatRFC3966 as d } from "./index-RFC3966.js";
function h(r, n) {
  var e = typeof Symbol < "u" && r[Symbol.iterator] || r["@@iterator"];
  if (e) return (e = e.call(r)).next.bind(e);
  if (Array.isArray(r) || (e = A(r)) || n) {
    e && (r = e);
    var t = 0;
    return function() {
      return t >= r.length ? { done: !0 } : { done: !1, value: r[t++] };
    };
  }
  throw new TypeError(`Invalid attempt to iterate non-iterable instance.
In order to be iterable, non-array objects must have a [Symbol.iterator]() method.`);
}
function A(r, n) {
  if (r) {
    if (typeof r == "string") return l(r, n);
    var e = Object.prototype.toString.call(r).slice(8, -1);
    if (e === "Object" && r.constructor && (e = r.constructor.name), e === "Map" || e === "Set") return Array.from(r);
    if (e === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(e)) return l(r, n);
  }
}
function l(r, n) {
  (n == null || n > r.length) && (n = r.length);
  for (var e = 0, t = new Array(n); e < n; e++)
    t[e] = r[e];
  return t;
}
function s(r, n) {
  var e = Object.keys(r);
  if (Object.getOwnPropertySymbols) {
    var t = Object.getOwnPropertySymbols(r);
    n && (t = t.filter(function(o) {
      return Object.getOwnPropertyDescriptor(r, o).enumerable;
    })), e.push.apply(e, t);
  }
  return e;
}
function y(r) {
  for (var n = 1; n < arguments.length; n++) {
    var e = arguments[n] != null ? arguments[n] : {};
    n % 2 ? s(Object(e), !0).forEach(function(t) {
      I(r, t, e[t]);
    }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(r, Object.getOwnPropertyDescriptors(e)) : s(Object(e)).forEach(function(t) {
      Object.defineProperty(r, t, Object.getOwnPropertyDescriptor(e, t));
    });
  }
  return r;
}
function I(r, n, e) {
  return n in r ? Object.defineProperty(r, n, { value: e, enumerable: !0, configurable: !0, writable: !0 }) : r[n] = e, r;
}
var m = {
  formatExtension: function(n, e, t) {
    return "".concat(n).concat(t.ext()).concat(e);
  }
};
function D(r, n, e, t) {
  if (e ? e = y(y({}, m), e) : e = m, t = new b(t), r.country && r.country !== "001") {
    if (!t.hasCountry(r.country))
      throw new Error("Unknown country: ".concat(r.country));
    t.country(r.country);
  } else if (r.countryCallingCode)
    t.selectNumberingPlan(r.countryCallingCode);
  else return r.phone || "";
  var o = t.countryCallingCode(), a = e.v2 ? r.nationalNumber : r.phone, c;
  switch (n) {
    case "NATIONAL":
      return a ? (c = f(a, r.carrierCode, "NATIONAL", t, e), u(c, r.ext, t, e.formatExtension)) : "";
    case "INTERNATIONAL":
      return a ? (c = f(a, null, "INTERNATIONAL", t, e), c = "+".concat(o, " ").concat(c), u(c, r.ext, t, e.formatExtension)) : "+".concat(o);
    case "E.164":
      return "+".concat(o).concat(a);
    case "RFC3966":
      return d({
        number: "+".concat(o).concat(a),
        ext: r.ext
      });
    // For reference, here's Google's IDD formatter:
    // https://github.com/google/libphonenumber/blob/32719cf74e68796788d1ca45abc85dcdc63ba5b9/java/libphonenumber/src/com/google/i18n/phonenumbers/PhoneNumberUtil.java#L1546
    // Not saying that this IDD formatter replicates it 1:1, but it seems to work.
    // Who would even need to format phone numbers in IDD format anyway?
    case "IDD":
      if (!e.fromCountry)
        return;
      var i = P(a, r.carrierCode, o, e.fromCountry, t);
      return u(i, r.ext, t, e.formatExtension);
    default:
      throw new Error('Unknown "format" argument passed to "formatNumber()": "'.concat(n, '"'));
  }
}
function f(r, n, e, t, o) {
  var a = w(t.formats(), r);
  return a ? O(r, a, {
    useInternationalFormat: e === "INTERNATIONAL",
    withNationalPrefix: !(a.nationalPrefixIsOptionalWhenFormattingInNationalFormat() && o && o.nationalPrefix === !1)
  }) : r;
}
function w(r, n) {
  for (var e = h(r), t; !(t = e()).done; ) {
    var o = t.value;
    if (o.leadingDigitsPatterns().length > 0) {
      var a = o.leadingDigitsPatterns()[o.leadingDigitsPatterns().length - 1];
      if (n.search(a) !== 0)
        continue;
    }
    if (g(n, o.pattern()))
      return o;
  }
}
function u(r, n, e, t) {
  return n ? t(r, n, e) : r;
}
function P(r, n, e, t, o) {
  var a = N(t, o.metadata);
  if (a === e) {
    var c = f(r, n, "NATIONAL", o);
    return e === "1" ? e + " " + c : c;
  }
  var i = v(t, void 0, o.metadata);
  if (i)
    return "".concat(i, " ").concat(e, " ").concat(f(r, null, "INTERNATIONAL", o));
}
export {
  w as chooseFormatForNumber,
  D as default
};
