/*!
 * inputmask.phone.extensions.js
 * https://github.com/RobinHerbots/Inputmask
 * Copyright (c) 2010 - 2017 Robin Herbots
 * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php)
 * Version: 3.3.11
 */

!(function (factory) {
  "function" == typeof define && define.amd
    ? define(
        ["./dependencyLibs/inputmask.dependencyLib", "./inputmask"],
        factory
      )
    : "object" == typeof exports
    ? (module.exports = factory(
        require("./dependencyLibs/inputmask.dependencyLib"),
        require("./inputmask")
      ))
    : factory(window.dependencyLib || jQuery, window.Inputmask);
})(function ($, Inputmask) {
  function maskSort(a, b) {
    var maska = (a.mask || a)
        .replace(/#/g, "9")
        .replace(/\)/, "9")
        .replace(/[+()#-]/g, ""),
      maskb = (b.mask || b)
        .replace(/#/g, "9")
        .replace(/\)/, "9")
        .replace(/[+()#-]/g, ""),
      maskas = (a.mask || a).split("#")[0],
      maskbs = (b.mask || b).split("#")[0];
    return 0 === maskbs.indexOf(maskas)
      ? -1
      : 0 === maskas.indexOf(maskbs)
      ? 1
      : maska.localeCompare(maskb);
  }
  var analyseMaskBase = Inputmask.prototype.analyseMask;
  return (
    (Inputmask.prototype.analyseMask = function (mask, regexMask, opts) {
      function reduceVariations(masks, previousVariation, previousmaskGroup) {
        (previousVariation = previousVariation || ""),
          (previousmaskGroup = previousmaskGroup || maskGroups),
          "" !== previousVariation &&
            (previousmaskGroup[previousVariation] = {});
        for (
          var variation = "",
            maskGroup =
              previousmaskGroup[previousVariation] || previousmaskGroup,
            i = masks.length - 1;
          i >= 0;
          i--
        )
          (maskGroup[
            (variation = (mask = masks[i].mask || masks[i]).substr(0, 1))
          ] = maskGroup[variation] || []),
            maskGroup[variation].unshift(mask.substr(1)),
            masks.splice(i, 1);
        for (var ndx in maskGroup)
          maskGroup[ndx].length > 500 &&
            reduceVariations(maskGroup[ndx].slice(), ndx, maskGroup);
      }
      function rebuild(maskGroup) {
        var mask = "",
          submasks = [];
        for (var ndx in maskGroup)
          $.isArray(maskGroup[ndx])
            ? 1 === maskGroup[ndx].length
              ? submasks.push(ndx + maskGroup[ndx])
              : submasks.push(
                  ndx +
                    opts.groupmarker.start +
                    maskGroup[ndx].join(
                      opts.groupmarker.end +
                        opts.alternatormarker +
                        opts.groupmarker.start
                    ) +
                    opts.groupmarker.end
                )
            : submasks.push(ndx + rebuild(maskGroup[ndx]));
        return (
          1 === submasks.length
            ? (mask += submasks[0])
            : (mask +=
                opts.groupmarker.start +
                submasks.join(
                  opts.groupmarker.end +
                    opts.alternatormarker +
                    opts.groupmarker.start
                ) +
                opts.groupmarker.end),
          mask
        );
      }
      var maskGroups = {};
      return (
        opts.phoneCodes &&
          (opts.phoneCodes &&
            opts.phoneCodes.length > 1e3 &&
            (reduceVariations(
              (mask = mask.substr(1, mask.length - 2)).split(
                opts.groupmarker.end +
                  opts.alternatormarker +
                  opts.groupmarker.start
              )
            ),
            (mask = rebuild(maskGroups))),
          (mask = mask.replace(/9/g, "\\9"))),
        analyseMaskBase.call(this, mask, regexMask, opts)
      );
    }),
    Inputmask.extendAliases({
      abstractphone: {
        groupmarker: {
          start: "<",
          end: ">",
        },
        countrycode: "",
        phoneCodes: [],
        mask: function (opts) {
          return (
            (opts.definitions = {
              "#": Inputmask.prototype.definitions[9],
            }),
            opts.phoneCodes.sort(maskSort)
          );
        },
        keepStatic: !0,
        onBeforeMask: function (value, opts) {
          var processedValue = value
            .replace(/^0{1,2}/, "")
            .replace(/[\s]/g, "");
          return (
            (processedValue.indexOf(opts.countrycode) > 1 ||
              -1 === processedValue.indexOf(opts.countrycode)) &&
              (processedValue = "+" + opts.countrycode + processedValue),
            processedValue
          );
        },
        onUnMask: function (maskedValue, unmaskedValue, opts) {
          return maskedValue.replace(/[()#-]/g, "");
        },
        inputmode: "tel",
      },
    }),
    Inputmask
  );
});
