import { jsxs as s, jsx as t, Fragment as ze } from "react/jsx-runtime";
import { useState as v, useMemo as De, useCallback as Fe, useEffect as oe } from "react";
import { useTranslation as Ue } from "react-i18next";
import { Users as ie, Building2 as le, Briefcase as He, MapPin as Be, Globe as Le, AlertCircle as Oe, X as D, Search as Xe, GitMerge as F, History as de, Plus as Ge, Loader2 as J, Check as Je, MoreHorizontal as _e, Edit2 as qe, Trash2 as We } from "lucide-react";
import { j as U, i as Ke, a as Qe, B as Ve } from "./index-CwSaRXXg.js";
import { u as Ye, T as Ze } from "./TenantScopeSelector-Dz7i1I43.js";
const et = (a) => ({
  get: (n, p) => U.get(n, {
    ...p,
    headers: a ? { ...p?.headers, "X-Tenant-Slug": a } : p?.headers
  }).then((r) => r.data),
  post: (n, p, r) => U.post(n, p, {
    ...r,
    headers: a ? { ...r?.headers, "X-Tenant-Slug": a } : r?.headers
  }).then((o) => o.data),
  put: (n, p, r) => U.put(n, p, {
    ...r,
    headers: a ? { ...r?.headers, "X-Tenant-Slug": a } : r?.headers
  }).then((o) => o.data),
  patch: (n, p, r) => U.patch(n, p, {
    ...r,
    headers: a ? { ...r?.headers, "X-Tenant-Slug": a } : r?.headers
  }).then((o) => o.data),
  delete: (n, p) => U.delete(n, {
    ...p,
    headers: a ? { ...p?.headers, "X-Tenant-Slug": a } : p?.headers
  }).then((r) => r.data)
}), _ = {
  Company: 0,
  Department: 1,
  JobTitle: 2,
  Office: 3
}, h = "/api/administration/users/references", tt = (a) => {
  const n = a ? et(a) : Ke, p = (r, o, l) => {
    const b = new URLSearchParams();
    o && b.append("tenantId", o), l && Object.entries(l).forEach(([x, f]) => {
      f !== void 0 && b.append(x, String(f));
    });
    const i = b.toString();
    return i ? `${h}/${r}?${i}` : `${h}/${r}`;
  };
  return {
    // === COMPANIES ===
    companies: {
      getAll: async (r, o, l, b, i = 1, x = 20) => {
        const f = p("companies", r, { search: o, isActive: l, source: b, page: i, pageSize: x });
        return n.get(f);
      },
      create: async (r, o) => {
        const l = p("companies", r);
        return n.post(l, o);
      },
      update: async (r, o) => {
        await n.put(`${h}/companies/${r}`, o);
      },
      activate: async (r) => {
        await n.patch(`${h}/companies/${r}/activate`);
      },
      deactivate: async (r) => {
        await n.patch(`${h}/companies/${r}/deactivate`);
      },
      delete: async (r) => {
        await n.delete(`${h}/companies/${r}`);
      }
    },
    // === DEPARTMENTS ===
    departments: {
      getAll: async (r, o, l, b, i = 1, x = 20) => {
        const f = p("departments", r, { search: o, isActive: l, source: b, page: i, pageSize: x });
        return n.get(f);
      },
      create: async (r, o) => {
        const l = p("departments", r);
        return n.post(l, o);
      },
      update: async (r, o) => {
        await n.put(`${h}/departments/${r}`, o);
      },
      activate: async (r) => {
        await n.patch(`${h}/departments/${r}/activate`);
      },
      deactivate: async (r) => {
        await n.patch(`${h}/departments/${r}/deactivate`);
      },
      delete: async (r) => {
        await n.delete(`${h}/departments/${r}`);
      }
    },
    // === JOB TITLES ===
    jobTitles: {
      getAll: async (r, o, l, b, i = 1, x = 20) => {
        const f = p("job-titles", r, { search: o, isActive: l, source: b, page: i, pageSize: x });
        return n.get(f);
      },
      create: async (r, o) => {
        const l = p("job-titles", r);
        return n.post(l, o);
      },
      update: async (r, o) => {
        await n.put(`${h}/job-titles/${r}`, o);
      },
      activate: async (r) => {
        await n.patch(`${h}/job-titles/${r}/activate`);
      },
      deactivate: async (r) => {
        await n.patch(`${h}/job-titles/${r}/deactivate`);
      },
      delete: async (r) => {
        await n.delete(`${h}/job-titles/${r}`);
      }
    },
    // === OFFICES ===
    offices: {
      getAll: async (r, o, l, b, i = 1, x = 20) => {
        const f = p("offices", r, { search: o, isActive: l, source: b, page: i, pageSize: x });
        return n.get(f);
      },
      create: async (r, o) => {
        const l = p("offices", r);
        return n.post(l, o);
      },
      update: async (r, o) => {
        await n.put(`${h}/offices/${r}`, o);
      },
      activate: async (r) => {
        await n.patch(`${h}/offices/${r}/activate`);
      },
      deactivate: async (r) => {
        await n.patch(`${h}/offices/${r}/deactivate`);
      },
      delete: async (r) => {
        await n.delete(`${h}/offices/${r}`);
      }
    },
    // === MERGE OPERATIONS ===
    merge: {
      execute: async (r) => n.post(`${h}/merge`, r),
      preview: async (r) => n.post(`${h}/merge/preview`, r),
      getHistory: async (r, o, l, b) => {
        const i = new URLSearchParams();
        r && i.append("tenantId", r), o !== void 0 && i.append("referenceType", String(o)), l && i.append("from", l), b && i.append("to", b);
        const x = i.toString(), f = x ? `${h}/merge/history?${x}` : `${h}/merge/history`;
        return n.get(f);
      }
    }
  };
}, me = tt;
function it() {
  const { t: a } = Ue("admin"), { currentTenant: n, userTenants: p, switchTenant: r } = Qe(), { tenants: o } = Ye(), [l, b] = v(
    n ? { tenantId: n.id, tenantSlug: n.slug, tenantUserId: "", tenantName: n.name } : "global"
  ), i = l === "global", x = i ? null : l.tenantId, f = i ? void 0 : l.tenantSlug, u = De(() => me(f), [f]), [w, pe] = v("departments"), [S, ve] = v([]), [q, V] = v(!0), [$, ue] = v(""), [H, he] = v(!1), [k, j] = v(/* @__PURE__ */ new Set()), [g, B] = v(1), [M] = v(20), [Y, be] = v(0), [A, fe] = v(0), [xe, L] = v(!1), [ge, O] = v(!1), [ye, X] = v(!1), [Ne, W] = v(!1), [I, Z] = v(null), [G, ee] = v(null), [te, we] = v([]), [K, z] = v(null), [m, C] = v({ code: "", name: "" }), [R, ae] = v(null), [re, se] = v(""), [P, E] = v(!1), [ne, N] = v(null), ce = [
    { id: "departments", label: a("references.tabs.departments", "Departments"), icon: ie, referenceType: _.Department },
    { id: "companies", label: a("references.tabs.companies", "Companies"), icon: le, referenceType: _.Company },
    { id: "jobTitles", label: a("references.tabs.jobTitles", "Job Titles"), icon: He, referenceType: _.JobTitle },
    { id: "offices", label: a("references.tabs.offices", "Offices"), icon: Be, referenceType: _.Office }
  ], Q = ce.find((e) => e.id === w), y = x, T = Fe(async () => {
    try {
      V(!0);
      const e = me(f ?? void 0), c = H ? void 0 : !0;
      let d;
      switch (w) {
        case "companies":
          d = await e.companies.getAll(y, $ || void 0, c, void 0, g, M);
          break;
        case "departments":
          d = await e.departments.getAll(y, $ || void 0, c, void 0, g, M);
          break;
        case "jobTitles":
          d = await e.jobTitles.getAll(y, $ || void 0, c, void 0, g, M);
          break;
        case "offices":
          d = await e.offices.getAll(y, $ || void 0, c, void 0, g, M);
          break;
        default:
          d = { items: [], totalCount: 0, page: 1, pageSize: 20, totalPages: 0, hasPreviousPage: !1, hasNextPage: !1 };
      }
      ve(d.items), be(d.totalCount), fe(d.totalPages), j(/* @__PURE__ */ new Set());
    } catch (e) {
      console.error("Failed to load references:", e), N("Failed to load references");
    } finally {
      V(!1);
    }
  }, [f, w, y, $, H, g, M]);
  oe(() => {
    T();
  }, [T]), oe(() => {
    B(1);
  }, [w, $, H, l]);
  const ke = (e) => {
    const c = new Set(k);
    c.has(e) ? c.delete(e) : c.add(e), j(c);
  }, Ce = () => {
    k.size === S.length ? j(/* @__PURE__ */ new Set()) : j(new Set(S.map((e) => e.id)));
  }, Te = async (e) => {
    try {
      await r(e);
      const c = p.find((d) => d.id === e);
      c && b({ tenantId: c.id, tenantSlug: c.slug, tenantUserId: "", tenantName: c.name });
    } catch (c) {
      console.error("Failed to switch tenant:", c);
    }
  }, Se = async () => {
    if (y)
      try {
        switch (E(!0), N(null), w) {
          case "companies":
            await u.companies.create(y, m);
            break;
          case "departments":
            await u.departments.create(y, m);
            break;
          case "jobTitles":
            await u.jobTitles.create(y, m);
            break;
          case "offices":
            await u.offices.create(y, m);
            break;
        }
        L(!1), C({ code: "", name: "" }), T();
      } catch (e) {
        const c = e instanceof Error ? e.message : "Failed to create";
        N(c);
      } finally {
        E(!1);
      }
  }, $e = async () => {
    if (I)
      try {
        E(!0), N(null);
        const e = {
          name: m.name,
          shortName: m.shortName,
          description: m.description
        };
        switch (w) {
          case "companies":
            await u.companies.update(I.id, e);
            break;
          case "departments":
            await u.departments.update(I.id, e);
            break;
          case "jobTitles":
            await u.jobTitles.update(I.id, e);
            break;
          case "offices":
            await u.offices.update(I.id, e);
            break;
        }
        O(!1), Z(null), T();
      } catch (e) {
        const c = e instanceof Error ? e.message : "Failed to update";
        N(c);
      } finally {
        E(!1);
      }
  }, Me = async (e) => {
    if (confirm(a("references.confirmDelete", "Are you sure you want to delete this reference?")))
      try {
        switch (w) {
          case "companies":
            await u.companies.delete(e);
            break;
          case "departments":
            await u.departments.delete(e);
            break;
          case "jobTitles":
            await u.jobTitles.delete(e);
            break;
          case "offices":
            await u.offices.delete(e);
            break;
        }
        T();
      } catch (c) {
        const d = c instanceof Error ? c.message : "Failed to delete";
        N(d);
      }
  }, je = async (e) => {
    try {
      if (e.isActive)
        switch (w) {
          case "companies":
            await u.companies.deactivate(e.id);
            break;
          case "departments":
            await u.departments.deactivate(e.id);
            break;
          case "jobTitles":
            await u.jobTitles.deactivate(e.id);
            break;
          case "offices":
            await u.offices.deactivate(e.id);
            break;
        }
      else
        switch (w) {
          case "companies":
            await u.companies.activate(e.id);
            break;
          case "departments":
            await u.departments.activate(e.id);
            break;
          case "jobTitles":
            await u.jobTitles.activate(e.id);
            break;
          case "offices":
            await u.offices.activate(e.id);
            break;
        }
      T();
    } catch (c) {
      const d = c instanceof Error ? c.message : "Failed to toggle status";
      N(d);
    }
  }, Ae = async () => {
    if (!(k.size < 2))
      try {
        const e = await u.merge.preview({
          tenantId: y,
          referenceType: Q.referenceType,
          sourceIds: Array.from(k)
        });
        ee(e), ae(null), se(""), X(!0);
      } catch (e) {
        console.error("Failed to get merge preview:", e), N("Failed to preview merge");
      }
  }, Ie = async () => {
    if (!R || !G) return;
    const e = Array.from(k).filter((c) => c !== R);
    try {
      E(!0), N(null), await u.merge.execute({
        tenantId: y,
        referenceType: Q.referenceType,
        targetId: R,
        sourceIds: e,
        reason: re || void 0
      }), X(!1), j(/* @__PURE__ */ new Set()), ee(null), T();
    } catch (c) {
      const d = c instanceof Error ? c.message : "Failed to merge";
      N(d);
    } finally {
      E(!1);
    }
  }, Re = async () => {
    try {
      const e = await u.merge.getHistory(y, Q.referenceType);
      we(e), W(!0);
    } catch (e) {
      console.error("Failed to load merge history:", e), N("Failed to load merge history");
    }
  }, Pe = (e) => {
    Z(e), C({
      code: e.code,
      name: e.name,
      shortName: e.shortName,
      description: e.description
    }), O(!0), z(null);
  }, Ee = (e) => e.isMerged ? /* @__PURE__ */ s("span", { className: "inline-flex items-center gap-1 px-2 py-1 text-xs font-medium rounded-full bg-[var(--info-bg)] text-[var(--info-text)]", children: [
    /* @__PURE__ */ t(F, { className: "w-3 h-3" }),
    a("references.merged", "Merged")
  ] }) : e.isActive ? null : /* @__PURE__ */ t("span", { className: "inline-flex items-center gap-1 px-2 py-1 text-xs font-medium rounded-full bg-[var(--bg-secondary)] text-[var(--text-secondary)]", children: a("references.inactive", "Inactive") });
  return /* @__PURE__ */ s("div", { className: "p-6", children: [
    /* @__PURE__ */ t("div", { className: "mb-6", children: /* @__PURE__ */ t(
      Ve,
      {
        items: [
          { label: a("admin.title", "Administration"), href: "/administration" },
          { label: a("users.title", "Users"), href: "/administration/users" },
          { label: a("references.title", "References") }
        ]
      }
    ) }),
    /* @__PURE__ */ s("div", { className: "mb-6", children: [
      /* @__PURE__ */ t("h1", { className: "text-2xl font-bold text-[var(--text-primary)]", children: a("references.title", "Reference Data Management") }),
      /* @__PURE__ */ t("p", { className: "text-[var(--text-secondary)] mt-1", children: a("references.description", "Manage companies, departments, job titles, and offices for your organization") }),
      /* @__PURE__ */ s("div", { className: "mt-4", children: [
        /* @__PURE__ */ t(
          Ze,
          {
            value: l,
            onChange: b,
            adminTenants: o.map((e) => ({
              tenantId: e.id,
              tenantSlug: e.slug,
              tenantName: e.name,
              tenantType: e.type
            }))
          }
        ),
        i && /* @__PURE__ */ s("p", { className: "mt-2 text-sm text-[var(--text-secondary)] flex items-center gap-1.5", children: [
          /* @__PURE__ */ t(Le, { className: "w-4 h-4" }),
          a("references.globalModeInfo", "Showing references from all tenants. Select a tenant to manage its data.")
        ] })
      ] })
    ] }),
    ne && /* @__PURE__ */ s("div", { className: "mb-4 p-4 bg-[var(--error-bg)] border border-[var(--error-border)] rounded-[var(--radius-card)]", children: [
      /* @__PURE__ */ s("div", { className: "flex items-center", children: [
        /* @__PURE__ */ t(Oe, { className: "w-5 h-5 text-[var(--error-text)]" }),
        /* @__PURE__ */ t("span", { className: "ml-2 text-[var(--error-text)]", children: ne }),
        /* @__PURE__ */ t("button", { onClick: () => N(null), className: "ml-auto text-[var(--error-text)]", children: /* @__PURE__ */ t(D, { className: "w-4 h-4" }) })
      ] }),
      /* @__PURE__ */ t(
        "button",
        {
          onClick: () => T(),
          className: "mt-2 text-sm text-[var(--error-text)] hover:opacity-80 underline",
          children: a("common.retry", "Retry")
        }
      )
    ] }),
    /* @__PURE__ */ t("div", { className: "border-b border-[var(--border-color)] mb-6", children: /* @__PURE__ */ t("nav", { className: "flex gap-4 -mb-px", children: ce.map((e) => {
      const c = e.icon, d = w === e.id;
      return /* @__PURE__ */ s(
        "button",
        {
          onClick: () => {
            pe(e.id), j(/* @__PURE__ */ new Set());
          },
          className: `flex items-center gap-2 px-4 py-3 border-b-2 font-medium text-sm transition-colors ${d ? "border-[var(--color-accent-500)] text-[var(--color-accent-600)]" : "border-transparent text-[var(--text-secondary)] hover:text-[var(--text-primary)] hover:border-[var(--border-color)]"}`,
          children: [
            /* @__PURE__ */ t(c, { className: "w-4 h-4" }),
            e.label
          ]
        },
        e.id
      );
    }) }) }),
    /* @__PURE__ */ s("div", { className: "flex items-center justify-between gap-4 mb-4", children: [
      /* @__PURE__ */ s("div", { className: "flex items-center gap-4", children: [
        /* @__PURE__ */ s("div", { className: "relative", children: [
          /* @__PURE__ */ t(Xe, { className: "absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-[var(--text-tertiary)]" }),
          /* @__PURE__ */ t(
            "input",
            {
              type: "text",
              value: $,
              onChange: (e) => ue(e.target.value),
              placeholder: a("common.search", "Search..."),
              className: "pl-10 pr-4 py-2 border border-[var(--border-color)] rounded-[var(--radius-input)] bg-[var(--bg-primary)] text-[var(--text-primary)] focus:ring-2 focus:ring-[var(--color-accent-500)]"
            }
          )
        ] }),
        /* @__PURE__ */ s("label", { className: "flex items-center gap-2 text-sm text-[var(--text-secondary)]", children: [
          /* @__PURE__ */ t(
            "input",
            {
              type: "checkbox",
              checked: H,
              onChange: (e) => he(e.target.checked),
              className: "rounded border-[var(--border-color)]"
            }
          ),
          a("references.showInactive", "Show inactive")
        ] })
      ] }),
      /* @__PURE__ */ s("div", { className: "flex items-center gap-2", children: [
        k.size >= 2 && !i && /* @__PURE__ */ s(
          "button",
          {
            onClick: Ae,
            className: "flex items-center gap-2 px-4 py-2 bg-[var(--info-dot)] text-white rounded-[var(--radius-button)] hover:opacity-90 transition-opacity",
            children: [
              /* @__PURE__ */ t(F, { className: "w-4 h-4" }),
              a("references.merge", "Merge"),
              " (",
              k.size,
              ")"
            ]
          }
        ),
        !i && /* @__PURE__ */ s(
          "button",
          {
            onClick: Re,
            className: "flex items-center gap-2 px-4 py-2 border border-[var(--border-color)] text-[var(--text-primary)] rounded-[var(--radius-button)] hover:bg-[var(--bg-hover)] transition-colors",
            children: [
              /* @__PURE__ */ t(de, { className: "w-4 h-4" }),
              a("references.history", "History")
            ]
          }
        ),
        /* @__PURE__ */ s(
          "button",
          {
            onClick: () => {
              C({ code: "", name: "" }), L(!0);
            },
            disabled: i,
            title: i ? a("references.selectTenantToCreate", "Select a tenant to create a reference") : void 0,
            className: `flex items-center gap-2 px-4 py-2 rounded-[var(--radius-button)] transition-colors ${i ? "bg-[var(--bg-secondary)] text-[var(--text-tertiary)] cursor-not-allowed" : "bg-[var(--color-accent-600)] text-white hover:bg-[var(--color-accent-700)]"}`,
            children: [
              /* @__PURE__ */ t(Ge, { className: "w-4 h-4" }),
              a("common.create", "Create")
            ]
          }
        )
      ] })
    ] }),
    /* @__PURE__ */ s("div", { className: "card overflow-hidden", children: [
      q && /* @__PURE__ */ t("div", { className: "flex items-center justify-center py-12", children: /* @__PURE__ */ t(J, { className: "w-8 h-8 animate-spin text-[var(--color-accent-500)]" }) }),
      !q && S.length === 0 && /* @__PURE__ */ t("div", { className: "text-center py-12 text-[var(--text-secondary)]", children: a("references.noItems", "No items found") }),
      !q && S.length > 0 && /* @__PURE__ */ s(ze, { children: [
        /* @__PURE__ */ s("table", { className: "w-full", children: [
          /* @__PURE__ */ t("thead", { className: "bg-[var(--bg-secondary)] border-b border-[var(--border-color)]", children: /* @__PURE__ */ s("tr", { children: [
            /* @__PURE__ */ t("th", { className: "w-12 px-4 py-3", children: !i && /* @__PURE__ */ t(
              "input",
              {
                type: "checkbox",
                checked: k.size === S.length && S.length > 0,
                onChange: Ce,
                className: "rounded border-[var(--border-color)]"
              }
            ) }),
            /* @__PURE__ */ t("th", { className: "px-4 py-3 text-left text-sm font-medium text-[var(--text-secondary)]", children: a("references.code", "Code") }),
            /* @__PURE__ */ t("th", { className: "px-4 py-3 text-left text-sm font-medium text-[var(--text-secondary)]", children: a("references.name", "Name") }),
            i && /* @__PURE__ */ t("th", { className: "px-4 py-3 text-left text-sm font-medium text-[var(--text-secondary)]", children: a("references.tenant", "Tenant") }),
            /* @__PURE__ */ t("th", { className: "px-4 py-3 text-left text-sm font-medium text-[var(--text-secondary)]", children: a("references.shortName", "Short Name") }),
            /* @__PURE__ */ t("th", { className: "px-4 py-3 text-center text-sm font-medium text-[var(--text-secondary)]", children: a("references.users", "Users") }),
            /* @__PURE__ */ t("th", { className: "px-4 py-3 text-center text-sm font-medium text-[var(--text-secondary)]", children: a("references.status", "Status") }),
            /* @__PURE__ */ t("th", { className: "w-20 px-4 py-3" })
          ] }) }),
          /* @__PURE__ */ t("tbody", { className: "divide-y divide-[var(--border-color)]", children: S.map((e) => /* @__PURE__ */ s(
            "tr",
            {
              className: `hover:bg-[var(--bg-hover)] transition-colors ${k.has(e.id) ? "bg-[var(--info-bg)]" : ""} ${e.isMerged ? "opacity-60" : ""}`,
              children: [
                /* @__PURE__ */ t("td", { className: "px-4 py-3", children: !i && /* @__PURE__ */ t(
                  "input",
                  {
                    type: "checkbox",
                    checked: k.has(e.id),
                    onChange: () => ke(e.id),
                    disabled: e.isMerged,
                    className: "rounded border-[var(--border-color)]"
                  }
                ) }),
                /* @__PURE__ */ t("td", { className: "px-4 py-3 font-mono text-sm text-[var(--text-primary)]", children: e.code }),
                /* @__PURE__ */ t("td", { className: "px-4 py-3 text-[var(--text-primary)]", children: e.name }),
                i && /* @__PURE__ */ t("td", { className: "px-4 py-3", children: e.tenantName ? /* @__PURE__ */ s(
                  "button",
                  {
                    onClick: () => e.tenantId && Te(e.tenantId),
                    className: "inline-flex items-center gap-1.5 px-2 py-1 text-xs font-medium rounded-full bg-[var(--info-bg)] text-[var(--info-text)] hover:opacity-80 transition-opacity",
                    title: a("references.switchToTenant", "Switch to this tenant"),
                    children: [
                      /* @__PURE__ */ t(le, { className: "w-3 h-3" }),
                      e.tenantName
                    ]
                  }
                ) : /* @__PURE__ */ t("span", { className: "text-[var(--text-tertiary)]", children: "-" }) }),
                /* @__PURE__ */ t("td", { className: "px-4 py-3 text-[var(--text-secondary)]", children: e.shortName || "-" }),
                /* @__PURE__ */ t("td", { className: "px-4 py-3 text-center", children: /* @__PURE__ */ s("span", { className: "inline-flex items-center gap-1 text-[var(--text-secondary)]", children: [
                  /* @__PURE__ */ t(ie, { className: "w-4 h-4" }),
                  e.userCount
                ] }) }),
                /* @__PURE__ */ s("td", { className: "px-4 py-3 text-center", children: [
                  Ee(e),
                  e.isActive && !e.isMerged && /* @__PURE__ */ s("span", { className: "inline-flex items-center gap-1 px-2 py-1 text-xs font-medium rounded-full bg-[var(--success-bg)] text-[var(--success-text)]", children: [
                    /* @__PURE__ */ t(Je, { className: "w-3 h-3" }),
                    a("references.active", "Active")
                  ] })
                ] }),
                /* @__PURE__ */ t("td", { className: "px-4 py-3", children: /* @__PURE__ */ s("div", { className: "relative", children: [
                  /* @__PURE__ */ t(
                    "button",
                    {
                      onClick: () => z(K === e.id ? null : e.id),
                      className: "p-2 hover:bg-[var(--bg-secondary)] rounded-[var(--radius-button)] transition-colors",
                      disabled: e.isMerged || i,
                      children: /* @__PURE__ */ t(_e, { className: "w-4 h-4" })
                    }
                  ),
                  K === e.id && /* @__PURE__ */ s("div", { className: "absolute right-0 top-full mt-1 w-48 bg-[var(--bg-primary)] border border-[var(--border-color)] rounded-[var(--radius-card)] shadow-lg z-10", children: [
                    /* @__PURE__ */ s(
                      "button",
                      {
                        onClick: () => Pe(e),
                        className: "flex items-center gap-2 w-full px-4 py-2 text-left text-sm text-[var(--text-primary)] hover:bg-[var(--bg-hover)]",
                        children: [
                          /* @__PURE__ */ t(qe, { className: "w-4 h-4" }),
                          a("common.edit", "Edit")
                        ]
                      }
                    ),
                    /* @__PURE__ */ t(
                      "button",
                      {
                        onClick: () => {
                          je(e), z(null);
                        },
                        className: "flex items-center gap-2 w-full px-4 py-2 text-left text-sm text-[var(--text-primary)] hover:bg-[var(--bg-hover)]",
                        children: e.isActive ? a("common.deactivate", "Deactivate") : a("common.activate", "Activate")
                      }
                    ),
                    e.userCount === 0 && /* @__PURE__ */ s(
                      "button",
                      {
                        onClick: () => {
                          Me(e.id), z(null);
                        },
                        className: "flex items-center gap-2 w-full px-4 py-2 text-left text-sm text-[var(--error-text)] hover:bg-[var(--error-bg)]",
                        children: [
                          /* @__PURE__ */ t(We, { className: "w-4 h-4" }),
                          a("common.delete", "Delete")
                        ]
                      }
                    )
                  ] })
                ] }) })
              ]
            },
            e.id
          )) })
        ] }),
        /* @__PURE__ */ s("div", { className: "flex items-center justify-between px-6 py-4 border-t border-[var(--border-color)] bg-[var(--bg-secondary)]", children: [
          /* @__PURE__ */ s("div", { className: "text-sm text-[var(--text-secondary)]", children: [
            a("references.showing", "Showing"),
            " ",
            (g - 1) * M + 1,
            " ",
            a("references.to", "to"),
            " ",
            Math.min(g * M, Y),
            " ",
            a("references.of", "of"),
            " ",
            Y,
            " ",
            a("references.results", "results")
          ] }),
          /* @__PURE__ */ s("div", { className: "flex items-center gap-2", children: [
            /* @__PURE__ */ t(
              "button",
              {
                onClick: () => B((e) => Math.max(1, e - 1)),
                disabled: g === 1,
                className: "px-3 py-1.5 border border-[var(--border-color)] rounded-[var(--radius-button)] text-[var(--text-primary)] hover:bg-[var(--bg-hover)] transition-colors disabled:opacity-50 disabled:cursor-not-allowed",
                children: a("common.previous", "Previous")
              }
            ),
            /* @__PURE__ */ t("div", { className: "flex items-center gap-1", children: Array.from({ length: Math.min(5, A) }, (e, c) => {
              let d;
              return A <= 5 || g <= 3 ? d = c + 1 : g >= A - 2 ? d = A - 4 + c : d = g - 2 + c, /* @__PURE__ */ t(
                "button",
                {
                  onClick: () => B(d),
                  className: `px-3 py-1.5 rounded-[var(--radius-button)] transition-colors ${g === d ? "bg-[var(--color-accent-600)] text-white" : "border border-[var(--border-color)] text-[var(--text-primary)] hover:bg-[var(--bg-hover)]"}`,
                  children: d
                },
                d
              );
            }) }),
            /* @__PURE__ */ t(
              "button",
              {
                onClick: () => B((e) => Math.min(A, e + 1)),
                disabled: g === A,
                className: "px-3 py-1.5 border border-[var(--border-color)] rounded-[var(--radius-button)] text-[var(--text-primary)] hover:bg-[var(--bg-hover)] transition-colors disabled:opacity-50 disabled:cursor-not-allowed",
                children: a("common.next", "Next")
              }
            )
          ] })
        ] })
      ] })
    ] }),
    xe && /* @__PURE__ */ t("div", { className: "fixed inset-0 bg-black/50 backdrop-blur-sm flex items-center justify-center z-50", children: /* @__PURE__ */ s("div", { className: "card w-full max-w-md p-6 m-4", children: [
      /* @__PURE__ */ s("div", { className: "flex items-center justify-between mb-4", children: [
        /* @__PURE__ */ t("h2", { className: "text-xl font-semibold text-[var(--text-primary)]", children: a("references.createTitle", "Create Reference") }),
        /* @__PURE__ */ t("button", { onClick: () => L(!1), className: "text-[var(--text-tertiary)] hover:text-[var(--text-primary)]", children: /* @__PURE__ */ t(D, { className: "w-5 h-5" }) })
      ] }),
      /* @__PURE__ */ s("div", { className: "space-y-4", children: [
        /* @__PURE__ */ s("div", { children: [
          /* @__PURE__ */ s("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: [
            a("references.code", "Code"),
            " *"
          ] }),
          /* @__PURE__ */ t(
            "input",
            {
              type: "text",
              value: m.code,
              onChange: (e) => C({ ...m, code: e.target.value }),
              className: "w-full px-4 py-2 border border-[var(--border-color)] rounded-[var(--radius-input)] bg-[var(--bg-primary)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-[var(--color-accent-500)]"
            }
          )
        ] }),
        /* @__PURE__ */ s("div", { children: [
          /* @__PURE__ */ s("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: [
            a("references.name", "Name"),
            " *"
          ] }),
          /* @__PURE__ */ t(
            "input",
            {
              type: "text",
              value: m.name,
              onChange: (e) => C({ ...m, name: e.target.value }),
              className: "w-full px-4 py-2 border border-[var(--border-color)] rounded-[var(--radius-input)] bg-[var(--bg-primary)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-[var(--color-accent-500)]"
            }
          )
        ] }),
        /* @__PURE__ */ s("div", { children: [
          /* @__PURE__ */ t("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: a("references.shortName", "Short Name") }),
          /* @__PURE__ */ t(
            "input",
            {
              type: "text",
              value: m.shortName || "",
              onChange: (e) => C({ ...m, shortName: e.target.value || null }),
              className: "w-full px-4 py-2 border border-[var(--border-color)] rounded-[var(--radius-input)] bg-[var(--bg-primary)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-[var(--color-accent-500)]"
            }
          )
        ] }),
        /* @__PURE__ */ s("div", { children: [
          /* @__PURE__ */ t("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: a("references.description", "Description") }),
          /* @__PURE__ */ t(
            "textarea",
            {
              value: m.description || "",
              onChange: (e) => C({ ...m, description: e.target.value || null }),
              rows: 3,
              className: "w-full px-4 py-2 border border-[var(--border-color)] rounded-[var(--radius-input)] bg-[var(--bg-primary)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-[var(--color-accent-500)]"
            }
          )
        ] })
      ] }),
      /* @__PURE__ */ s("div", { className: "flex justify-end gap-3 mt-6", children: [
        /* @__PURE__ */ t(
          "button",
          {
            onClick: () => L(!1),
            className: "px-4 py-2 rounded-[var(--radius-button)] bg-[var(--bg-secondary)] text-[var(--text-secondary)] hover:bg-[var(--bg-hover)] transition-colors",
            children: a("common.cancel", "Cancel")
          }
        ),
        /* @__PURE__ */ s(
          "button",
          {
            onClick: Se,
            disabled: P || !m.code || !m.name,
            className: "flex items-center gap-2 px-4 py-2 rounded-[var(--radius-button)] bg-[var(--color-accent-600)] text-white hover:bg-[var(--color-accent-700)] transition-colors disabled:opacity-50",
            children: [
              P && /* @__PURE__ */ t(J, { className: "w-4 h-4 animate-spin" }),
              a("common.create", "Create")
            ]
          }
        )
      ] })
    ] }) }),
    ge && I && /* @__PURE__ */ t("div", { className: "fixed inset-0 bg-black/50 backdrop-blur-sm flex items-center justify-center z-50", children: /* @__PURE__ */ s("div", { className: "card w-full max-w-md p-6 m-4", children: [
      /* @__PURE__ */ s("div", { className: "flex items-center justify-between mb-4", children: [
        /* @__PURE__ */ t("h2", { className: "text-xl font-semibold text-[var(--text-primary)]", children: a("references.editTitle", "Edit Reference") }),
        /* @__PURE__ */ t("button", { onClick: () => O(!1), className: "text-[var(--text-tertiary)] hover:text-[var(--text-primary)]", children: /* @__PURE__ */ t(D, { className: "w-5 h-5" }) })
      ] }),
      /* @__PURE__ */ s("div", { className: "space-y-4", children: [
        /* @__PURE__ */ s("div", { children: [
          /* @__PURE__ */ t("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: a("references.code", "Code") }),
          /* @__PURE__ */ t(
            "input",
            {
              type: "text",
              value: m.code,
              disabled: !0,
              className: "w-full px-4 py-2 border border-[var(--border-color)] rounded-[var(--radius-input)] bg-[var(--bg-secondary)] text-[var(--text-tertiary)] cursor-not-allowed"
            }
          )
        ] }),
        /* @__PURE__ */ s("div", { children: [
          /* @__PURE__ */ s("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: [
            a("references.name", "Name"),
            " *"
          ] }),
          /* @__PURE__ */ t(
            "input",
            {
              type: "text",
              value: m.name,
              onChange: (e) => C({ ...m, name: e.target.value }),
              className: "w-full px-4 py-2 border border-[var(--border-color)] rounded-[var(--radius-input)] bg-[var(--bg-primary)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-[var(--color-accent-500)]"
            }
          )
        ] }),
        /* @__PURE__ */ s("div", { children: [
          /* @__PURE__ */ t("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: a("references.shortName", "Short Name") }),
          /* @__PURE__ */ t(
            "input",
            {
              type: "text",
              value: m.shortName || "",
              onChange: (e) => C({ ...m, shortName: e.target.value || null }),
              className: "w-full px-4 py-2 border border-[var(--border-color)] rounded-[var(--radius-input)] bg-[var(--bg-primary)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-[var(--color-accent-500)]"
            }
          )
        ] }),
        /* @__PURE__ */ s("div", { children: [
          /* @__PURE__ */ t("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: a("references.description", "Description") }),
          /* @__PURE__ */ t(
            "textarea",
            {
              value: m.description || "",
              onChange: (e) => C({ ...m, description: e.target.value || null }),
              rows: 3,
              className: "w-full px-4 py-2 border border-[var(--border-color)] rounded-[var(--radius-input)] bg-[var(--bg-primary)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-[var(--color-accent-500)]"
            }
          )
        ] })
      ] }),
      /* @__PURE__ */ s("div", { className: "flex justify-end gap-3 mt-6", children: [
        /* @__PURE__ */ t(
          "button",
          {
            onClick: () => O(!1),
            className: "px-4 py-2 rounded-[var(--radius-button)] bg-[var(--bg-secondary)] text-[var(--text-secondary)] hover:bg-[var(--bg-hover)] transition-colors",
            children: a("common.cancel", "Cancel")
          }
        ),
        /* @__PURE__ */ s(
          "button",
          {
            onClick: $e,
            disabled: P || !m.name,
            className: "flex items-center gap-2 px-4 py-2 rounded-[var(--radius-button)] bg-[var(--color-accent-600)] text-white hover:bg-[var(--color-accent-700)] transition-colors disabled:opacity-50",
            children: [
              P && /* @__PURE__ */ t(J, { className: "w-4 h-4 animate-spin" }),
              a("common.save", "Save")
            ]
          }
        )
      ] })
    ] }) }),
    ye && G && /* @__PURE__ */ t("div", { className: "fixed inset-0 bg-black/50 backdrop-blur-sm flex items-center justify-center z-50", children: /* @__PURE__ */ s("div", { className: "card w-full max-w-lg p-6 m-4", children: [
      /* @__PURE__ */ s("div", { className: "flex items-center justify-between mb-4", children: [
        /* @__PURE__ */ s("h2", { className: "text-xl font-semibold text-[var(--text-primary)] flex items-center gap-2", children: [
          /* @__PURE__ */ t(F, { className: "w-5 h-5 text-[var(--info-dot)]" }),
          a("references.mergeTitle", "Merge References")
        ] }),
        /* @__PURE__ */ t("button", { onClick: () => X(!1), className: "text-[var(--text-tertiary)] hover:text-[var(--text-primary)]", children: /* @__PURE__ */ t(D, { className: "w-5 h-5" }) })
      ] }),
      /* @__PURE__ */ t("p", { className: "text-sm text-[var(--text-secondary)] mb-4", children: a("references.mergeDescription", "Select the target reference. All other selected references will be merged into it and their users will be reassigned.") }),
      /* @__PURE__ */ s("div", { className: "space-y-2 mb-4", children: [
        /* @__PURE__ */ t("label", { className: "block text-sm font-medium text-[var(--text-secondary)]", children: a("references.selectTarget", "Select target reference") }),
        G.sources.map((e) => /* @__PURE__ */ s(
          "label",
          {
            className: `flex items-center gap-3 p-3 border rounded-[var(--radius-card)] cursor-pointer transition-colors ${R === e.id ? "border-[var(--info-dot)] bg-[var(--info-bg)]" : "border-[var(--border-color)] hover:border-[var(--color-accent-500)]"}`,
            children: [
              /* @__PURE__ */ t(
                "input",
                {
                  type: "radio",
                  name: "mergeTarget",
                  value: e.id,
                  checked: R === e.id,
                  onChange: () => ae(e.id),
                  className: "text-[var(--info-dot)]"
                }
              ),
              /* @__PURE__ */ s("div", { className: "flex-1", children: [
                /* @__PURE__ */ t("div", { className: "font-medium text-[var(--text-primary)]", children: e.name }),
                /* @__PURE__ */ s("div", { className: "text-sm text-[var(--text-secondary)]", children: [
                  e.code,
                  " ",
                  e.shortName && `(${e.shortName})`
                ] })
              ] }),
              /* @__PURE__ */ s("span", { className: "text-sm text-[var(--text-secondary)]", children: [
                e.userCount,
                " ",
                a("references.users", "users")
              ] })
            ]
          },
          e.id
        ))
      ] }),
      /* @__PURE__ */ s("div", { className: "mb-4", children: [
        /* @__PURE__ */ t("label", { className: "block text-sm font-medium text-[var(--text-secondary)] mb-1", children: a("references.mergeReason", "Reason (optional)") }),
        /* @__PURE__ */ t(
          "textarea",
          {
            value: re,
            onChange: (e) => se(e.target.value),
            rows: 2,
            placeholder: a("references.mergeReasonPlaceholder", "Why are you merging these references?"),
            className: "w-full px-4 py-2 border border-[var(--border-color)] rounded-[var(--radius-input)] bg-[var(--bg-primary)] text-[var(--text-primary)] focus:outline-none focus:ring-2 focus:ring-[var(--color-accent-500)]"
          }
        )
      ] }),
      /* @__PURE__ */ t("div", { className: "p-3 bg-[var(--warning-bg)] border border-[var(--warning-border)] rounded-[var(--radius-card)] mb-4", children: /* @__PURE__ */ s("p", { className: "text-sm text-[var(--warning-text)]", children: [
        /* @__PURE__ */ t("strong", { children: G.totalUsersAffected }),
        " ",
        a("references.usersWillBeReassigned", "users will be reassigned")
      ] }) }),
      /* @__PURE__ */ s("div", { className: "flex justify-end gap-3", children: [
        /* @__PURE__ */ t(
          "button",
          {
            onClick: () => X(!1),
            className: "px-4 py-2 rounded-[var(--radius-button)] bg-[var(--bg-secondary)] text-[var(--text-secondary)] hover:bg-[var(--bg-hover)] transition-colors",
            children: a("common.cancel", "Cancel")
          }
        ),
        /* @__PURE__ */ s(
          "button",
          {
            onClick: Ie,
            disabled: P || !R,
            className: "flex items-center gap-2 px-4 py-2 rounded-[var(--radius-button)] bg-[var(--info-dot)] text-white hover:opacity-90 transition-opacity disabled:opacity-50",
            children: [
              P && /* @__PURE__ */ t(J, { className: "w-4 h-4 animate-spin" }),
              /* @__PURE__ */ t(F, { className: "w-4 h-4" }),
              a("references.merge", "Merge")
            ]
          }
        )
      ] })
    ] }) }),
    Ne && /* @__PURE__ */ t("div", { className: "fixed inset-0 bg-black/50 backdrop-blur-sm flex items-center justify-center z-50", children: /* @__PURE__ */ s("div", { className: "card w-full max-w-2xl p-6 m-4 max-h-[80vh] overflow-hidden flex flex-col", children: [
      /* @__PURE__ */ s("div", { className: "flex items-center justify-between mb-4", children: [
        /* @__PURE__ */ s("h2", { className: "text-xl font-semibold text-[var(--text-primary)] flex items-center gap-2", children: [
          /* @__PURE__ */ t(de, { className: "w-5 h-5" }),
          a("references.historyTitle", "Merge History")
        ] }),
        /* @__PURE__ */ t("button", { onClick: () => W(!1), className: "text-[var(--text-tertiary)] hover:text-[var(--text-primary)]", children: /* @__PURE__ */ t(D, { className: "w-5 h-5" }) })
      ] }),
      /* @__PURE__ */ t("div", { className: "overflow-y-auto flex-1", children: te.length === 0 ? /* @__PURE__ */ t("div", { className: "text-center py-8 text-[var(--text-secondary)]", children: a("references.noHistory", "No merge history found") }) : /* @__PURE__ */ t("div", { className: "space-y-3", children: te.map((e) => /* @__PURE__ */ s(
        "div",
        {
          className: "p-4 border border-[var(--border-color)] rounded-[var(--radius-card)]",
          children: [
            /* @__PURE__ */ s("div", { className: "flex items-center gap-2 mb-2", children: [
              /* @__PURE__ */ t(F, { className: "w-4 h-4 text-[var(--info-dot)]" }),
              /* @__PURE__ */ t("span", { className: "font-medium text-[var(--text-primary)]", children: e.sourceReferenceName }),
              /* @__PURE__ */ t("span", { className: "text-[var(--text-secondary)]", children: "→" }),
              /* @__PURE__ */ t("span", { className: "font-medium text-[var(--text-primary)]", children: e.targetReferenceName })
            ] }),
            /* @__PURE__ */ s("div", { className: "text-sm text-[var(--text-secondary)] space-y-1", children: [
              /* @__PURE__ */ s("div", { children: [
                e.affectedUserCount,
                " users reassigned"
              ] }),
              /* @__PURE__ */ s("div", { children: [
                "By ",
                e.mergedByUserName
              ] }),
              /* @__PURE__ */ t("div", { children: new Date(e.mergedAt).toLocaleString() }),
              e.mergeReason && /* @__PURE__ */ s("div", { className: "italic", children: [
                '"',
                e.mergeReason,
                '"'
              ] })
            ] })
          ]
        },
        e.id
      )) }) }),
      /* @__PURE__ */ t("div", { className: "flex justify-end mt-4 pt-4 border-t border-[var(--border-color)]", children: /* @__PURE__ */ t(
        "button",
        {
          onClick: () => W(!1),
          className: "px-4 py-2 rounded-[var(--radius-button)] bg-[var(--bg-secondary)] text-[var(--text-secondary)] hover:bg-[var(--bg-hover)] transition-colors",
          children: a("common.close", "Close")
        }
      ) })
    ] }) }),
    K && /* @__PURE__ */ t(
      "button",
      {
        type: "button",
        className: "fixed inset-0 z-0",
        onClick: () => z(null),
        "aria-label": "Close menu"
      }
    )
  ] });
}
export {
  it as ReferencesManagementPage
};
//# sourceMappingURL=ReferencesManagementPage-eFsKjIEK.js.map
