(function initFivaDetectFieldType(global) {
  "use strict";

  if (global.FIVA?.DetectFieldType) {
    if (typeof module !== "undefined" && module.exports) module.exports = global.FIVA.DetectFieldType;
    return;
  }

  const FIELD_TYPES = Object.freeze({
    dropdown: "dropdown",
    textbox: "textbox",
    textarea: "textarea",
    checkbox: "checkbox",
    radio: "radio"
  });

  const TEXTBOX_TYPES = new Set(["text", "email", "date", "tel"]);

  function detectFieldType(control) {
    if (!control?.tagName) return "";
    const tagName = String(control.tagName || "").toUpperCase();
    if (tagName === "SELECT") return FIELD_TYPES.dropdown;
    if (tagName === "TEXTAREA") return FIELD_TYPES.textarea;
    if (tagName !== "INPUT") return "";

    const inputType = String(control.type || "text").toLowerCase();
    if (inputType === "checkbox") return FIELD_TYPES.checkbox;
    if (inputType === "radio") return FIELD_TYPES.radio;
    if (TEXTBOX_TYPES.has(inputType)) return FIELD_TYPES.textbox;
    return "";
  }

  const DetectFieldType = Object.freeze({
    FIELD_TYPES,
    detectFieldType,
    TEXTBOX_TYPES
  });

  global.FIVA = Object.assign(global.FIVA || {}, { DetectFieldType });

  if (typeof module !== "undefined" && module.exports) {
    module.exports = DetectFieldType;
  }
})(typeof globalThis !== "undefined" ? globalThis : this);
