import { h as attr_class, d as stringify, aj as attr, l as escape_html, an as attr_style, ak as bind_props, n as derived } from './index-BHoOpAe1.js';

/* empty css                                          */
function validateInput(inputValue, dataType, validPattern, inProgressPattern, customValidators) {
  let validationResult = "Valid";
  switch (dataType) {
    case "email":
      validationResult = validateEmailInput(inputValue);
      break;
    case "tel":
      validationResult = validPattern === null ? validatePhoneNumber(inputValue) : validateTextWithPattern(inputValue, validPattern, inProgressPattern);
      break;
    case "password":
      validationResult = validatePassword(inputValue, validPattern, inProgressPattern);
      break;
    case "text":
      validationResult = validateTextWithPattern(inputValue, validPattern, inProgressPattern);
      break;
  }
  customValidators.forEach((validator) => {
    const currentResult = validator(inputValue, validationResult);
    if (currentResult === "Invalid") {
      validationResult = "Invalid";
    } else if (currentResult === "InProgress") {
      validationResult = "InProgress";
    } else {
      validationResult = validationResult === "Valid" && currentResult === "Valid" ? "Valid" : validationResult;
    }
  });
  return validationResult;
}
function validateTextWithPattern(inputValue, validationPattern, inProgressPattern) {
  if (validationPattern !== null) {
    if (validationPattern.test(inputValue)) {
      return "Valid";
    } else if (inProgressPattern !== null && inProgressPattern.test(inputValue) || inputValue.length === 0) {
      return "InProgress";
    } else {
      return "Invalid";
    }
  }
  return "Valid";
}
function validatePassword(password, validationPattern, inProgressPattern) {
  if (validationPattern !== null) {
    if (validationPattern.test(password)) {
      return "Valid";
    } else if (inProgressPattern !== null && inProgressPattern.test(password) && password.length === 0) {
      return "InProgress";
    } else {
      return "Invalid";
    }
  }
  return "Valid";
}
function validateEmailInput(emailId) {
  try {
    const validPattern = new RegExp(/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/);
    const inProgressPattern = new RegExp(/[^ ]{1,}[\w0-9.,_,@]{0,}/);
    if (validPattern.test(emailId)) {
      return "Valid";
    } else if (inProgressPattern.test(emailId) || emailId.length === 0) {
      return "InProgress";
    } else {
      return "Invalid";
    }
  } catch (e) {
    console.error("Email Regex creation failed: ", e);
  }
  return "Valid";
}
function validatePhoneNumber(phoneNumber) {
  try {
    const validationPattern = new RegExp("^[6-9]{1}[0-9]{9}$");
    const inProgressPattern = new RegExp("^[6-9]{1}[0-9]{0,9}$");
    if (validationPattern.test(phoneNumber)) {
      return "Valid";
    } else if (inProgressPattern.test(phoneNumber) || phoneNumber.length === 0) {
      return "InProgress";
    } else {
      return "Invalid";
    }
  } catch (e) {
    console.error("Phone Regex creation failed", e);
  }
  return phoneNumber.length === 10 ? "Valid" : phoneNumber.length > 10 ? "InProgress" : "Invalid";
}
function Input($$renderer, $$props) {
  $$renderer.component(($$renderer2) => {
    let {
      value = "",
      placeholder = "",
      dataType = "text",
      label = "",
      onErrorMessage = "",
      infoMessage = "",
      validators = [],
      disable = false,
      validationPattern = null,
      inProgressPattern = null,
      addFocusColor = false,
      maxLength = 1e3,
      minLength = 0,
      min,
      max,
      actionInput = false,
      useTextArea = false,
      autoComplete = "on",
      name = "",
      testId = "",
      textTransformers = [],
      textViewPresentation = [],
      onFocus = () => {
      },
      onFocusout = () => {
      },
      onBlur = () => {
      },
      onInput = () => {
      },
      onPaste = () => {
      },
      onStateChange = () => {
      },
      onClick = () => {
      },
      onKeyDown = () => {
      },
      classes,
      role,
      ariaExpanded,
      ariaAutocomplete,
      ariaControls,
      ariaActivedescendant
    } = $$props;
    function focus() {
      try {
        inputElement?.focus();
        inputElement?.scrollIntoView({ behavior: "smooth", block: "center" });
      } catch (error) {
        console.error("Error focusing or scrolling inputElement:", error);
      }
    }
    function blur() {
      try {
        inputElement?.blur();
      } catch (error) {
        console.error("Error blurring inputElement:", error);
      }
    }
    function getInputRef() {
      return inputElement;
    }
    let inputElement = null;
    let validationState = derived(() => {
      const valueValidation = validateInput(value, dataType, validationPattern, inProgressPattern, validators);
      if (valueValidation === "InProgress" && value.length > 0 && inputElement !== null) ;
      return valueValidation;
    });
    const showErrorMessage = derived(() => validationState() === "Invalid");
    $$renderer2.push(`<div${attr_class(`input-container ${stringify(classes ?? "")}`, "svelte-1w3nj1e", {
      "input-error": validationState() === "Invalid" && !actionInput
    })}>`);
    if (typeof label === "string" && label !== "" && !actionInput) {
      $$renderer2.push("<!--[0-->");
      $$renderer2.push(`<label class="label svelte-1w3nj1e"${attr("for", name)}>${escape_html(label)}</label>`);
    } else {
      $$renderer2.push("<!--[-1-->");
    }
    $$renderer2.push(`<!--]--> `);
    if (useTextArea) {
      $$renderer2.push("<!--[0-->");
      $$renderer2.push(`<textarea${attr("placeholder", placeholder)}${attr("autocomplete", autoComplete)}${attr("name", name)}${attr("role", role)}${attr("aria-expanded", ariaExpanded)}${attr("aria-autocomplete", ariaAutocomplete)}${attr("aria-controls", ariaControls)}${attr("aria-activedescendant", ariaActivedescendant)}${attr_style(`--focus-border: ${stringify(addFocusColor ? 1 : 0)}px;`)}${attr("disabled", disable, true)}${attr("maxlength", dataType === "tel" ? null : maxLength)}${attr("minlength", minLength)}${attr_class("svelte-1w3nj1e", void 0, { "action-input": actionInput })}>`);
      const $$body = escape_html(value);
      if ($$body) {
        $$renderer2.push(`${$$body}`);
      }
      $$renderer2.push(`</textarea>`);
    } else {
      $$renderer2.push("<!--[-1-->");
      $$renderer2.push(`<input${attr("type", dataType)}${attr("value", value)}${attr("placeholder", placeholder)}${attr("autocomplete", autoComplete)}${attr("name", name)}${attr("role", role)}${attr("aria-expanded", ariaExpanded)}${attr("aria-autocomplete", ariaAutocomplete)}${attr("aria-controls", ariaControls)}${attr("aria-activedescendant", ariaActivedescendant)}${attr("data-pw", testId)}${attr("disabled", disable, true)}${attr("maxlength", dataType === "tel" ? null : maxLength)}${attr("minlength", minLength)}${attr("min", min)}${attr("max", max)}${attr_class("svelte-1w3nj1e", void 0, { "action-input": actionInput })}/>`);
    }
    $$renderer2.push(`<!--]--> `);
    if (onErrorMessage !== "" && showErrorMessage() && !actionInput) {
      $$renderer2.push("<!--[0-->");
      $$renderer2.push(`<div class="error-message svelte-1w3nj1e">${escape_html(onErrorMessage)}</div>`);
    } else {
      $$renderer2.push("<!--[-1-->");
    }
    $$renderer2.push(`<!--]--> `);
    if (infoMessage !== "" && !actionInput) {
      $$renderer2.push("<!--[0-->");
      $$renderer2.push(`<div class="info-message svelte-1w3nj1e">${escape_html(infoMessage)}</div>`);
    } else {
      $$renderer2.push("<!--[-1-->");
    }
    $$renderer2.push(`<!--]--></div>`);
    bind_props($$props, { value, focus, blur, getInputRef });
  });
}

export { Input as I };
//# sourceMappingURL=Input-M7rFN1IJ.js.map
