// src/lib/phone-validation.ts
function normalizeEthiopiaPhone(input) {
  const digits = input.replace(/\D/g, "");
  if (digits.length < 9) {
    return "";
  }
  return `+251${digits.slice(-9)}`;
}
var createPhoneField = (config) => {
  const phoneRegex = config.phone.phoneRegex || /^\+251[79]\d{8}$/;
  const regex = typeof phoneRegex === "string" ? new RegExp(phoneRegex) : phoneRegex;
  return {
    normalize: normalizeEthiopiaPhone,
    validate: (phone) => {
      const n = normalizeEthiopiaPhone(phone);
      if (!n) {
        return false;
      }
      return regex.test(n);
    },
    regex
  };
};

// src/lib/phone-match.ts
var digitsOnly = (value) => value.replace(/\D/g, "");
var safeEqual = (a, b) => {
  if (a.length !== b.length) {
    return false;
  }
  let diff = 0;
  for (let i = 0; i < a.length; i++) {
    diff |= a.charCodeAt(i) ^ b.charCodeAt(i);
  }
  return diff === 0;
};
function phonesMatch(storedPhone, telegramPhone, config) {
  const storedDigits = digitsOnly(storedPhone);
  const telegramDigits = digitsOnly(telegramPhone);
  if (!(storedDigits && telegramDigits)) {
    return false;
  }
  if (safeEqual(storedDigits, telegramDigits)) {
    return true;
  }
  const pv = createPhoneField(config);
  const storedEth = normalizeEthiopiaPhone(storedPhone);
  const telegramEth = normalizeEthiopiaPhone(
    telegramPhone.startsWith("+") ? telegramPhone : `+${telegramPhone}`
  );
  if (storedEth && telegramEth && pv.validate(storedEth) && pv.validate(telegramEth) && storedEth === telegramEth) {
    return true;
  }
  const withPlus = telegramPhone.startsWith("+") ? telegramPhone : `+${telegramDigits}`;
  if (storedPhone === withPlus || storedEth === withPlus) {
    return true;
  }
  return false;
}
var phoneDigitsForLookup = (phone) => digitsOnly(phone);
export {
  phoneDigitsForLookup,
  phonesMatch
};
//# sourceMappingURL=phone-match.js.map