(function initCreatorGender(root, factory) {
  const api = factory();
  if (typeof module === "object" && module.exports) module.exports = api;
  root.StonkCreatorGender = api;
})(typeof globalThis !== "undefined" ? globalThis : this, function creatorGenderFactory() {
  "use strict";

  const VERSION = "vn-name-v2";
  const FEMALE_NAMES = new Set([
    "thao", "huong", "nhung", "huyen", "quynh", "thuy", "diem", "oanh", "mai", "lan",
    "nga", "tram", "trinh", "vy", "my", "ngan", "nhi", "thu", "anhthu", "dieulinh",
  ]);
  const MALE_NAMES = new Set([
    "tuan", "quang", "huy", "hoang", "duc", "phong", "kien", "son", "long", "cuong",
    "dat", "hieu", "nam", "viet", "phuc", "trung", "loc", "vu", "manh", "thang",
  ]);
  const FEMALE_SIGNALS = ["girl", "female", "lady", "miss", "mama", "mom", "mebim", "mebe", "nangtho", "chidẹp", "chidep"];
  const MALE_SIGNALS = ["boy", "male", "gentleman", "mister", "papa", "dad", "bobe", "menstyle"];

  function normalize(value) {
    return String(value || "")
      .normalize("NFD")
      .replace(/[\u0300-\u036f]/g, "")
      .replace(/đ/g, "d")
      .replace(/Đ/g, "D")
      .toLowerCase()
      .replace(/[^a-z0-9]+/g, " ")
      .trim();
  }

  function unknown(source = "insufficient") {
    return { creatorGender: "unknown", confidence: 0, source, algorithmVersion: VERSION };
  }

  function classify(input) {
    const raw = typeof input === "object" && input !== null
      ? [
          input.nickname, input.nickName, input.name, input.handle, input.expertAccount, input.creator_handle,
          input.creator?.nickname, input.creator?.nick_name, input.creator?.name, input.creator?.handle,
        ].filter(Boolean).join(" ")
      : String(input || "");
    const text = normalize(raw);
    if (!text) return unknown();
    const signalTokens = text.split(/\s+/).filter(Boolean);
    const hasSignal = signal => {
      const normalized = normalize(signal).replace(/\s+/g, "");
      return signalTokens.some(token => token === normalized || (normalized.length >= 4 && token.startsWith(normalized)));
    };
    const femaleSignal = FEMALE_SIGNALS.find(hasSignal);
    const maleSignal = MALE_SIGNALS.find(hasSignal);
    if (femaleSignal && !maleSignal) return { creatorGender: "female", confidence: 0.94, source: `explicit:${femaleSignal}`, algorithmVersion: VERSION };
    if (maleSignal && !femaleSignal) return { creatorGender: "male", confidence: 0.94, source: `explicit:${maleSignal}`, algorithmVersion: VERSION };

    const tokens = text.split(/\s+/).filter(token => token.length >= 2 && !/^\d+$/.test(token));
    const femaleHits = tokens.filter(token => FEMALE_NAMES.has(token));
    const maleHits = tokens.filter(token => MALE_NAMES.has(token));
    if (femaleHits.length && !maleHits.length) return { creatorGender: "female", confidence: 0.74, source: `name:${femaleHits[0]}`, algorithmVersion: VERSION };
    if (maleHits.length && !femaleHits.length) return { creatorGender: "male", confidence: 0.74, source: `name:${maleHits[0]}`, algorithmVersion: VERSION };
    return unknown(femaleHits.length || maleHits.length ? "conflicting-name" : "insufficient");
  }

  function apply(record) {
    if (record?.creatorGenderManual || ["male", "female"].includes(String(record?.creatorGender || "").toLowerCase())) {
      return record;
    }
    const inferred = classify(record);
    return {
      ...(record || {}),
      creatorGender: inferred.creatorGender,
      creatorGenderConfidence: inferred.confidence,
      creatorGenderSource: inferred.source,
      creatorGenderVersion: inferred.algorithmVersion,
    };
  }

  return { VERSION, classify, apply };
});
