import { useMemo } from "react";
import { useTranslation } from "react-i18next";
import {
  generateMLKEM768KeyPair,
  generatePassword,
  generateRealityKeyPair,
  generateRealityShortId,
} from "../generate";
import {
  CERT_MODES,
  ENCRYPTION_MODES,
  ENCRYPTION_RTT,
  ENCRYPTION_TYPES,
  FINGERPRINTS,
  FLOWS,
  multiplexLevels,
  SECURITY,
  SS_CIPHERS,
  TRANSPORTS,
  TUIC_CONGESTION,
  TUIC_UDP_RELAY_MODES,
  XHTTP_MODES,
} from "./constants";
import type { FieldConfig } from "./types";

export function useProtocolFields() {
  const { t } = useTranslation("servers");

  return useMemo<Record<string, FieldConfig[]>>(
    () => ({
      shadowsocks: [
        {
          name: "ratio",
          type: "number",
          label: t("traffic_ratio", "Ratio"),
          min: 0,
          step: 0.01,
          defaultValue: 1,
          group: "basic",
        },
        {
          name: "port",
          type: "number",
          label: t("port", "Port"),
          min: 1,
          max: 65_535,
          placeholder: "1-65535",
          group: "basic",
        },
        {
          name: "cipher",
          type: "select",
          label: t("cipher", "Encryption Algorithm"),
          options: SS_CIPHERS,
          defaultValue: "chacha20-ietf-poly1305",
          group: "basic",
        },
        {
          name: "server_key",
          type: "input",
          label: t("server_key", "Server Key"),
          generate: {
            function: () => generatePassword(32),
          },
          group: "basic",
          condition: (p) =>
            [
              "2022-blake3-aes-128-gcm",
              "2022-blake3-aes-256-gcm",
              "2022-blake3-chacha20-poly1305",
            ].includes(p.cipher),
        },
        {
          name: "obfs",
          type: "select",
          label: t("obfs", "Obfuscation"),
          options: ["none", "http", "tls"],
          defaultValue: "none",
          group: "obfs",
        },
        {
          name: "obfs_host",
          type: "input",
          label: t("host", "Host"),
          placeholder: "e.g. www.bing.com",
          group: "obfs",
          condition: (p) => p.obfs && p.obfs !== "none",
        },
        {
          name: "obfs_path",
          type: "input",
          label: t("path", "Path"),
          placeholder: "e.g. /path/to/obfs",
          group: "obfs",
          condition: (p) => p.obfs && p.obfs !== "none",
        },
        {
          name: "sni",
          type: "input",
          label: t("security_sni", "SNI"),
          group: "security",
          condition: (p) => p.obfs === "tls",
        },
        {
          name: "allow_insecure",
          type: "switch",
          label: t("security_allow_insecure", "Allow Insecure"),
          group: "security",
          condition: (p) => p.obfs === "tls",
        },
        {
          name: "cert_mode",
          type: "select",
          label: t("cert_mode", "Certificate Mode"),
          options: CERT_MODES,
          defaultValue: "none",
          group: "security",
          condition: (p) => p.obfs === "tls",
        },
        {
          name: "cert_dns_provider",
          type: "input",
          label: t("cert_dns_provider", "DNS Provider"),
          placeholder: "e.g. cloudflare, aliyun",
          group: "security",
          condition: (p) => p.obfs === "tls" && p.cert_mode === "dns",
        },
        {
          name: "cert_dns_env",
          type: "textarea",
          label: t("cert_dns_env", "DNS Environment Variables"),
          placeholder:
            "CF_DNS_API_TOKEN=1234567890abcdefghijklmnopqrstuvwxyz\nALI_ACCESS_KEY_ID=your_access_key_id\nALI_ACCESS_KEY_SECRET=your_access_key_secret",
          group: "security",
          condition: (p) => p.obfs === "tls" && p.cert_mode === "dns",
        },
      ],
      vmess: [
        {
          name: "ratio",
          type: "number",
          label: t("traffic_ratio", "Ratio"),
          min: 0,
          step: 0.01,
          defaultValue: 1,
          group: "basic",
        },
        {
          name: "port",
          type: "number",
          label: t("port", "Port"),
          min: 1,
          max: 65_535,
          placeholder: "1-65535",
          group: "basic",
        },
        {
          name: "transport",
          type: "select",
          label: t("transport", "Transport"),
          options: TRANSPORTS.vmess,
          defaultValue: "tcp",
          group: "transport",
        },
        {
          name: "security",
          type: "select",
          label: t("security", "Security"),
          options: SECURITY.vmess,
          defaultValue: "none",
          group: "security",
        },
        {
          name: "host",
          type: "input",
          label: t("host", "Host"),
          placeholder: "e.g. www.bing.com",
          group: "transport",
          condition: (p) =>
            ["websocket", "xhttp", "httpupgrade"].includes(p.transport),
        },
        {
          name: "path",
          type: "input",
          label: t("path", "Path"),
          placeholder: "e.g. /path/to/obfs",
          group: "transport",
          condition: (p) =>
            ["websocket", "xhttp", "httpupgrade"].includes(p.transport),
        },
        {
          name: "service_name",
          type: "input",
          label: t("service_name", "Service Name"),
          group: "transport",
          condition: (p) => p.transport === "grpc",
        },
        {
          name: "sni",
          type: "input",
          label: t("security_sni", "SNI"),
          group: "security",
          condition: (p) => p.security !== "none",
        },
        {
          name: "allow_insecure",
          type: "switch",
          label: t("security_allow_insecure", "Allow Insecure"),
          group: "security",
          condition: (p) => p.security !== "none",
        },
        {
          name: "fingerprint",
          type: "select",
          label: t("security_fingerprint", "Fingerprint"),
          options: FINGERPRINTS,
          defaultValue: "chrome",
          group: "security",
          condition: (p) => p.security !== "none",
        },
        {
          name: "cert_mode",
          type: "select",
          label: t("cert_mode", "Certificate Mode"),
          options: CERT_MODES,
          defaultValue: "none",
          group: "security",
          condition: (p) => p.security === "tls",
        },
        {
          name: "cert_dns_provider",
          type: "input",
          label: t("cert_dns_provider", "DNS Provider"),
          placeholder: "e.g. cloudflare, aliyun",
          group: "security",
          condition: (p) => p.security === "tls" && p.cert_mode === "dns",
        },
        {
          name: "cert_dns_env",
          type: "textarea",
          label: t("cert_dns_env", "DNS Environment Variables"),
          placeholder:
            "CF_DNS_API_TOKEN=1234567890abcdefghijklmnopqrstuvwxyz\nALI_ACCESS_KEY_ID=your_access_key_id\nALI_ACCESS_KEY_SECRET=your_access_key_secret",
          group: "security",
          condition: (p) => p.security === "tls" && p.cert_mode === "dns",
        },
      ],
      vless: [
        {
          name: "ratio",
          type: "number",
          label: t("traffic_ratio", "Ratio"),
          min: 0,
          step: 0.01,
          defaultValue: 1,
          group: "basic",
        },
        {
          name: "port",
          type: "number",
          label: t("port", "Port"),
          min: 1,
          max: 65_535,
          placeholder: "1-65535",
          group: "basic",
        },
        {
          name: "transport",
          type: "select",
          label: t("transport", "Transport"),
          options: TRANSPORTS.vless,
          defaultValue: "tcp",
          group: "transport",
        },
        {
          name: "flow",
          type: "select",
          label: t("flow", "Flow"),
          options: FLOWS.vless,
          defaultValue: "none",
          group: "transport",
          condition: (p) =>
            p.encryption === "mlkem768x25519plus" ||
            (p.transport === "tcp" &&
              (p.security === "tls" || p.security === "reality")),
        },
        {
          name: "security",
          type: "select",
          label: t("security", "Security"),
          options: SECURITY.vless,
          defaultValue: "none",
          group: "security",
        },
        {
          name: "host",
          type: "input",
          label: t("host", "Host"),
          placeholder: "e.g. www.bing.com",
          group: "transport",
          condition: (p) =>
            ["websocket", "mkcp", "httpupgrade", "xhttp"].includes(p.transport),
        },
        {
          name: "path",
          type: "input",
          label: t("path", "Path"),
          placeholder: "e.g. /path/to/obfs",
          group: "transport",
          condition: (p) =>
            ["websocket", "mkcp", "httpupgrade", "xhttp"].includes(p.transport),
        },
        {
          name: "service_name",
          type: "input",
          label: t("service_name", "Service Name"),
          group: "transport",
          condition: (p) => p.transport === "grpc",
        },
        {
          name: "xhttp_mode",
          type: "select",
          label: t("mode", "Mode"),
          options: XHTTP_MODES,
          defaultValue: "auto",
          group: "transport",
          condition: (p) => p.transport === "xhttp",
        },
        {
          name: "xhttp_extra",
          type: "textarea",
          label: t("extra", "Extra"),
          placeholder: "{}",
          group: "transport",
          condition: (p) => p.transport === "xhttp",
        },
        {
          name: "sni",
          type: "input",
          label: t("security_sni", "SNI"),
          group: "security",
          condition: (p) => p.security !== "none",
        },
        {
          name: "allow_insecure",
          type: "switch",
          label: t("security_allow_insecure", "Allow Insecure"),
          group: "security",
          condition: (p) => p.security !== "none",
        },
        {
          name: "fingerprint",
          type: "select",
          label: t("security_fingerprint", "Fingerprint"),
          options: FINGERPRINTS,
          defaultValue: "chrome",
          group: "security",
          condition: (p) => p.security !== "none",
        },
        {
          name: "reality_server_addr",
          type: "input",
          label: t("security_server_address", "Reality Server Address"),
          placeholder: t(
            "security_server_address_placeholder",
            "e.g. 1.2.3.4 or domain"
          ),
          group: "reality",
          condition: (p) => p.security === "reality",
        },
        {
          name: "reality_server_port",
          type: "number",
          label: t("security_server_port", "Reality Server Port"),
          min: 1,
          max: 65_535,
          placeholder: "1-65535",
          group: "reality",
          condition: (p) => p.security === "reality",
        },
        {
          name: "reality_private_key",
          type: "input",
          label: t("security_private_key", "Reality Private Key"),
          placeholder: t(
            "security_private_key_placeholder",
            "Enter private key"
          ),
          group: "reality",
          generate: {
            function: generateRealityKeyPair,
            updateFields: {
              reality_private_key: "privateKey",
              reality_public_key: "publicKey",
            } as Record<string, string>,
          },
          condition: (p) => p.security === "reality",
        },
        {
          name: "reality_public_key",
          type: "input",
          label: t("security_public_key", "Reality Public Key"),
          placeholder: t("security_public_key_placeholder", "Enter public key"),
          group: "reality",
          condition: (p) => p.security === "reality",
        },
        {
          name: "reality_short_id",
          type: "input",
          label: t("security_short_id", "Reality Short ID"),
          group: "reality",
          generate: {
            function: generateRealityShortId,
          },
          condition: (p) => p.security === "reality",
        },
        {
          name: "encryption",
          type: "select",
          label: t("encryption", "Encryption"),
          options: ENCRYPTION_TYPES,
          defaultValue: "none",
          group: "encryption",
        },
        {
          name: "encryption_mode",
          type: "select",
          label: t("encryption_mode", "Mode"),
          options: ENCRYPTION_MODES,
          defaultValue: "native",
          group: "encryption",
          condition: (p) => p.encryption === "mlkem768x25519plus",
        },
        {
          name: "encryption_rtt",
          type: "select",
          label: t("encryption_rtt", "RTT"),
          options: ENCRYPTION_RTT,
          defaultValue: "1rtt",
          group: "encryption",
          condition: (p) => p.encryption === "mlkem768x25519plus",
        },
        {
          name: "encryption_ticket",
          type: "input",
          label: t("encryption_ticket", "Ticket Time"),
          placeholder: "e.g. 600",
          group: "encryption",
          condition: (p) =>
            p.encryption === "mlkem768x25519plus" &&
            p.encryption_rtt === "0rtt",
        },
        {
          name: "encryption_server_padding",
          type: "input",
          label: t("encryption_server_padding", "Server Padding"),
          placeholder: "e.g. 100-111-1111.75-0-111.50-0-3333",
          group: "encryption",
          condition: (p) => p.encryption === "mlkem768x25519plus",
        },
        {
          name: "encryption_private_key",
          type: "input",
          label: t("encryption_private_key", "Private Key"),
          placeholder: t(
            "encryption_private_key_placeholder",
            "Leave empty for auto-generation"
          ),
          group: "encryption",
          generate: {
            functions: [
              {
                label: t(
                  "generate_standard_encryption_key",
                  "Generate Standard Key"
                ),
                function: generateRealityKeyPair,
              },
              {
                label: t(
                  "generate_quantum_resistant_key",
                  "Generate Quantum-Resistant Key"
                ),
                function: generateMLKEM768KeyPair,
              },
            ],
            updateFields: {
              encryption_private_key: "privateKey",
              encryption_password: "publicKey",
            } as Record<string, string>,
          },
          condition: (p) => p.encryption === "mlkem768x25519plus",
        },
        {
          name: "encryption_client_padding",
          type: "input",
          label: t("encryption_client_padding", "Client Padding"),
          placeholder: "e.g. 100-111-1111.75-0-111.50-0-3333",
          group: "encryption",
          condition: (p) => p.encryption === "mlkem768x25519plus",
        },
        {
          name: "encryption_password",
          type: "input",
          label: t("encryption_password", "Password"),
          placeholder: t(
            "encryption_password_placeholder",
            "Leave empty for auto-generation"
          ),
          group: "encryption",
          condition: (p) => p.encryption === "mlkem768x25519plus",
        },
        {
          name: "cert_mode",
          type: "select",
          label: t("cert_mode", "Certificate Mode"),
          options: CERT_MODES,
          defaultValue: "none",
          group: "security",
          condition: (p) => p.security === "tls",
        },
        {
          name: "cert_dns_provider",
          type: "input",
          label: t("cert_dns_provider", "DNS Provider"),
          placeholder: "e.g. cloudflare, aliyun",
          group: "security",
          condition: (p) => p.security === "tls" && p.cert_mode === "dns",
        },
        {
          name: "cert_dns_env",
          type: "textarea",
          label: t("cert_dns_env", "DNS Environment Variables"),
          placeholder:
            "CF_DNS_API_TOKEN=1234567890abcdefghijklmnopqrstuvwxyz\nALI_ACCESS_KEY_ID=your_access_key_id\nALI_ACCESS_KEY_SECRET=your_access_key_secret",
          group: "security",
          condition: (p) => p.security === "tls" && p.cert_mode === "dns",
        },
      ],
      trojan: [
        {
          name: "ratio",
          type: "number",
          label: t("traffic_ratio", "Ratio"),
          min: 0,
          step: 0.01,
          defaultValue: 1,
          group: "basic",
        },
        {
          name: "port",
          type: "number",
          label: t("port", "Port"),
          min: 1,
          max: 65_535,
          placeholder: "1-65535",
          group: "basic",
        },
        {
          name: "transport",
          type: "select",
          label: t("transport", "Transport"),
          options: TRANSPORTS.trojan,
          defaultValue: "tcp",
          group: "transport",
        },
        {
          name: "security",
          type: "select",
          label: t("security", "Security"),
          options: SECURITY.trojan,
          defaultValue: "tls",
          group: "security",
        },
        {
          name: "host",
          type: "input",
          label: t("host", "Host"),
          placeholder: "e.g. www.bing.com",
          group: "transport",
          condition: (p) =>
            ["websocket", "xhttp", "httpupgrade"].includes(p.transport),
        },
        {
          name: "path",
          type: "input",
          label: t("path", "Path"),
          placeholder: "e.g. /path/to/obfs",
          group: "transport",
          condition: (p) =>
            ["websocket", "xhttp", "httpupgrade"].includes(p.transport),
        },
        {
          name: "service_name",
          type: "input",
          label: t("service_name", "Service Name"),
          group: "transport",
          condition: (p) => p.transport === "grpc",
        },
        {
          name: "sni",
          type: "input",
          label: t("security_sni", "SNI"),
          group: "security",
          condition: (p) => p.security !== "none",
        },
        {
          name: "allow_insecure",
          type: "switch",
          label: t("security_allow_insecure", "Allow Insecure"),
          group: "security",
          condition: (p) => p.security !== "none",
        },
        {
          name: "fingerprint",
          type: "select",
          label: t("security_fingerprint", "Fingerprint"),
          options: FINGERPRINTS,
          defaultValue: "chrome",
          group: "security",
          condition: (p) => p.security !== "none",
        },
        {
          name: "cert_mode",
          type: "select",
          label: t("cert_mode", "Certificate Mode"),
          options: CERT_MODES,
          defaultValue: "none",
          group: "security",
          condition: (p) => p.security === "tls",
        },
        {
          name: "cert_dns_provider",
          type: "input",
          label: t("cert_dns_provider", "DNS Provider"),
          placeholder: "e.g. cloudflare, aliyun",
          group: "security",
          condition: (p) => p.security === "tls" && p.cert_mode === "dns",
        },
        {
          name: "cert_dns_env",
          type: "textarea",
          label: t("cert_dns_env", "DNS Environment Variables"),
          placeholder:
            "CF_DNS_API_TOKEN=1234567890abcdefghijklmnopqrstuvwxyz\nALI_ACCESS_KEY_ID=your_access_key_id\nALI_ACCESS_KEY_SECRET=your_access_key_secret",
          group: "security",
          condition: (p) => p.security === "tls" && p.cert_mode === "dns",
        },
      ],
      hysteria: [
        {
          name: "ratio",
          type: "number",
          label: t("traffic_ratio", "Ratio"),
          min: 0,
          step: 0.01,
          defaultValue: 1,
          group: "basic",
        },
        {
          name: "port",
          type: "number",
          label: t("port", "Port"),
          min: 1,
          max: 65_535,
          placeholder: "1-65535",
          group: "basic",
        },
        {
          name: "hop_ports",
          type: "input",
          label: t("hop_ports", "Hop Ports"),
          placeholder: t("hop_ports_placeholder", "e.g. 1-65535"),
          group: "basic",
        },
        {
          name: "hop_interval",
          type: "number",
          label: t("hop_interval", "Hop Interval"),
          placeholder: "e.g. 300",
          min: 1,
          suffix: "S",
          group: "basic",
        },
        {
          name: "obfs",
          type: "select",
          label: t("obfs", "Obfuscation"),
          options: ["none", "salamander"],
          defaultValue: "none",
          group: "obfs",
        },
        {
          name: "obfs_password",
          type: "input",
          label: t("obfs_password", "Obfuscation Password"),
          placeholder: t(
            "obfs_password_placeholder",
            "Enter obfuscation password"
          ),
          generate: {
            function: () => generatePassword(16),
          },
          group: "obfs",
          condition: (p) => p.obfs && p.obfs !== "none",
        },
        {
          name: "up_mbps",
          type: "number",
          label: t("up_mbps", "Upload Bandwidth"),
          min: 1,
          placeholder: t(
            "bandwidth_placeholder",
            "Enter bandwidth, leave empty for BBR"
          ),
          suffix: "Mbps",
          group: "basic",
        },
        {
          name: "down_mbps",
          type: "number",
          label: t("down_mbps", "Download Bandwidth"),
          min: 1,
          placeholder: t(
            "bandwidth_placeholder",
            "Enter bandwidth, leave empty for BBR"
          ),
          suffix: "Mbps",
          group: "basic",
        },
        {
          name: "sni",
          type: "input",
          label: t("security_sni", "SNI"),
          group: "security",
        },
        {
          name: "allow_insecure",
          type: "switch",
          label: t("security_allow_insecure", "Allow Insecure"),
          group: "security",
        },
        {
          name: "fingerprint",
          type: "select",
          label: t("security_fingerprint", "Fingerprint"),
          options: FINGERPRINTS,
          defaultValue: "chrome",
          group: "security",
        },
        {
          name: "cert_mode",
          type: "select",
          label: t("cert_mode", "Certificate Mode"),
          options: CERT_MODES,
          defaultValue: "none",
          group: "security",
        },
        {
          name: "cert_dns_provider",
          type: "input",
          label: t("cert_dns_provider", "DNS Provider"),
          placeholder: "e.g. cloudflare, aliyun",
          group: "security",
          condition: (p) => p.cert_mode === "dns",
        },
        {
          name: "cert_dns_env",
          type: "textarea",
          label: t("cert_dns_env", "DNS Environment Variables"),
          placeholder:
            "CF_DNS_API_TOKEN=1234567890abcdefghijklmnopqrstuvwxyz\nALI_ACCESS_KEY_ID=your_access_key_id\nALI_ACCESS_KEY_SECRET=your_access_key_secret",
          group: "security",
          condition: (p) => p.cert_mode === "dns",
        },
      ],
      tuic: [
        {
          name: "ratio",
          type: "number",
          label: t("traffic_ratio", "Ratio"),
          min: 0,
          step: 0.01,
          defaultValue: 1,
          group: "basic",
        },
        {
          name: "port",
          type: "number",
          label: t("port", "Port"),
          min: 1,
          max: 65_535,
          placeholder: "1-65535",
          group: "basic",
        },
        {
          name: "udp_relay_mode",
          type: "select",
          label: t("udp_relay_mode", "UDP Relay Mode"),
          options: TUIC_UDP_RELAY_MODES,
          defaultValue: "native",
          group: "basic",
        },
        {
          name: "congestion_controller",
          type: "select",
          label: t("congestion_controller", "Congestion Controller"),
          options: TUIC_CONGESTION,
          defaultValue: "bbr",
          group: "basic",
        },
        {
          name: "disable_sni",
          type: "switch",
          label: t("disable_sni", "Disable SNI"),
          group: "basic",
        },
        {
          name: "reduce_rtt",
          type: "switch",
          label: t("reduce_rtt", "Reduce RTT"),
          group: "basic",
        },
        {
          name: "sni",
          type: "input",
          label: t("security_sni", "SNI"),
          group: "security",
        },
        {
          name: "allow_insecure",
          type: "switch",
          label: t("security_allow_insecure", "Allow Insecure"),
          group: "security",
        },
        {
          name: "fingerprint",
          type: "select",
          label: t("security_fingerprint", "Fingerprint"),
          options: FINGERPRINTS,
          defaultValue: "chrome",
          group: "security",
        },
        {
          name: "cert_mode",
          type: "select",
          label: t("cert_mode", "Certificate Mode"),
          options: CERT_MODES,
          defaultValue: "none",
          group: "security",
        },
        {
          name: "cert_dns_provider",
          type: "input",
          label: t("cert_dns_provider", "DNS Provider"),
          placeholder: "e.g. cloudflare, aliyun",
          group: "security",
          condition: (p) => p.cert_mode === "dns",
        },
        {
          name: "cert_dns_env",
          type: "textarea",
          label: t("cert_dns_env", "DNS Environment Variables"),
          placeholder:
            "CF_DNS_API_TOKEN=1234567890abcdefghijklmnopqrstuvwxyz\nALI_ACCESS_KEY_ID=your_access_key_id\nALI_ACCESS_KEY_SECRET=your_access_key_secret",
          group: "security",
          condition: (p) => p.cert_mode === "dns",
        },
      ],
      socks: [
        {
          name: "ratio",
          type: "number",
          label: t("traffic_ratio", "Ratio"),
          min: 0,
          step: 0.01,
          defaultValue: 1,
          group: "basic",
        },
        {
          name: "port",
          type: "number",
          label: t("port", "Port"),
          min: 1,
          max: 65_535,
          placeholder: "1-65535",
          group: "basic",
        },
      ],
      naive: [
        {
          name: "ratio",
          type: "number",
          label: t("traffic_ratio", "Ratio"),
          min: 0,
          step: 0.01,
          defaultValue: 1,
          group: "basic",
        },
        {
          name: "port",
          type: "number",
          label: t("port", "Port"),
          min: 1,
          max: 65_535,
          placeholder: "1-65535",
          group: "basic",
        },
        {
          name: "security",
          type: "select",
          label: t("security", "Security"),
          options: SECURITY.naive,
          defaultValue: "none",
          group: "security",
        },
        {
          name: "sni",
          type: "input",
          label: t("security_sni", "SNI"),
          group: "security",
          condition: (p) => p.security !== "none",
        },
        {
          name: "allow_insecure",
          type: "switch",
          label: t("security_allow_insecure", "Allow Insecure"),
          group: "security",
          condition: (p) => p.security !== "none",
        },
        {
          name: "fingerprint",
          type: "select",
          label: t("security_fingerprint", "Fingerprint"),
          options: FINGERPRINTS,
          defaultValue: "chrome",
          group: "security",
          condition: (p) => p.security !== "none",
        },
        {
          name: "cert_mode",
          type: "select",
          label: t("cert_mode", "Certificate Mode"),
          options: CERT_MODES,
          defaultValue: "none",
          group: "security",
          condition: (p) => p.security === "tls",
        },
        {
          name: "cert_dns_provider",
          type: "input",
          label: t("cert_dns_provider", "DNS Provider"),
          placeholder: "e.g. cloudflare, aliyun",
          group: "security",
          condition: (p) => p.security === "tls" && p.cert_mode === "dns",
        },
        {
          name: "cert_dns_env",
          type: "textarea",
          label: t("cert_dns_env", "DNS Environment Variables"),
          placeholder:
            "CF_DNS_API_TOKEN=1234567890abcdefghijklmnopqrstuvwxyz\nALI_ACCESS_KEY_ID=your_access_key_id\nALI_ACCESS_KEY_SECRET=your_access_key_secret",
          group: "security",
          condition: (p) => p.security === "tls" && p.cert_mode === "dns",
        },
      ],
      http: [
        {
          name: "ratio",
          type: "number",
          label: t("traffic_ratio", "Ratio"),
          min: 0,
          step: 0.01,
          defaultValue: 1,
          group: "basic",
        },
        {
          name: "port",
          type: "number",
          label: t("port", "Port"),
          min: 1,
          max: 65_535,
          placeholder: "1-65535",
          group: "basic",
        },
        {
          name: "security",
          type: "select",
          label: t("security", "Security"),
          options: SECURITY.http,
          defaultValue: "none",
          group: "security",
        },
        {
          name: "sni",
          type: "input",
          label: t("security_sni", "SNI"),
          group: "security",
          condition: (p) => p.security !== "none",
        },
        {
          name: "allow_insecure",
          type: "switch",
          label: t("security_allow_insecure", "Allow Insecure"),
          group: "security",
          condition: (p) => p.security !== "none",
        },
        {
          name: "fingerprint",
          type: "select",
          label: t("security_fingerprint", "Fingerprint"),
          options: FINGERPRINTS,
          defaultValue: "chrome",
          group: "security",
          condition: (p) => p.security !== "none",
        },
        {
          name: "cert_mode",
          type: "select",
          label: t("cert_mode", "Certificate Mode"),
          options: CERT_MODES,
          defaultValue: "none",
          group: "security",
          condition: (p) => p.security === "tls",
        },
        {
          name: "cert_dns_provider",
          type: "input",
          label: t("cert_dns_provider", "DNS Provider"),
          placeholder: "e.g. cloudflare, aliyun",
          group: "security",
          condition: (p) => p.security === "tls" && p.cert_mode === "dns",
        },
        {
          name: "cert_dns_env",
          type: "textarea",
          label: t("cert_dns_env", "DNS Environment Variables"),
          placeholder:
            "CF_DNS_API_TOKEN=1234567890abcdefghijklmnopqrstuvwxyz\nALI_ACCESS_KEY_ID=your_access_key_id\nALI_ACCESS_KEY_SECRET=your_access_key_secret",
          group: "security",
          condition: (p) => p.security === "tls" && p.cert_mode === "dns",
        },
      ],
      mieru: [
        {
          name: "ratio",
          type: "number",
          label: t("traffic_ratio", "Ratio"),
          min: 0,
          step: 0.01,
          defaultValue: 1,
          group: "basic",
        },
        {
          name: "port",
          type: "number",
          label: t("port", "Port"),
          min: 1,
          max: 65_535,
          placeholder: "1-65535",
          group: "basic",
        },
        {
          name: "multiplex",
          type: "select",
          label: t("multiplex", "Multiplex"),
          options: multiplexLevels,
          defaultValue: "off",
          group: "basic",
        },
        {
          name: "transport",
          type: "select",
          label: t("transport", "Transport"),
          options: TRANSPORTS.mieru,
          defaultValue: "tcp",
          group: "transport",
        },
      ],
      anytls: [
        {
          name: "ratio",
          type: "number",
          label: t("traffic_ratio", "Ratio"),
          min: 0,
          step: 0.01,
          defaultValue: 1,
          group: "basic",
        },
        {
          name: "port",
          type: "number",
          label: t("port", "Port"),
          min: 1,
          max: 65_535,
          placeholder: "1-65535",
          group: "basic",
        },
        {
          name: "security",
          type: "select",
          label: t("security", "Security"),
          options: SECURITY.anytls,
          defaultValue: "tls",
          group: "security",
        },
        {
          name: "padding_scheme",
          type: "textarea",
          label: t("padding_scheme", "Padding Scheme"),
          placeholder: t(
            "padding_scheme_placeholder",
            "One padding rule per line, format: stop=8, 0=30-30"
          ),
          group: "basic",
        },
        {
          name: "sni",
          type: "input",
          label: t("security_sni", "SNI"),
          group: "security",
          condition: (p) => p.security !== "none",
        },
        {
          name: "allow_insecure",
          type: "switch",
          label: t("security_allow_insecure", "Allow Insecure"),
          group: "security",
          condition: (p) => p.security !== "none",
        },
        {
          name: "fingerprint",
          type: "select",
          label: t("security_fingerprint", "Fingerprint"),
          options: FINGERPRINTS,
          defaultValue: "chrome",
          group: "security",
          condition: (p) => p.security !== "none",
        },
        {
          name: "cert_mode",
          type: "select",
          label: t("cert_mode", "Certificate Mode"),
          options: CERT_MODES,
          defaultValue: "none",
          group: "security",
          condition: (p) => p.security === "tls",
        },
        {
          name: "cert_dns_provider",
          type: "input",
          label: t("cert_dns_provider", "DNS Provider"),
          placeholder: "e.g. cloudflare, aliyun",
          group: "security",
          condition: (p) => p.cert_mode === "dns",
        },
        {
          name: "cert_dns_env",
          type: "textarea",
          label: t("cert_dns_env", "DNS Environment Variables"),
          placeholder:
            "CF_DNS_API_TOKEN=1234567890abcdefghijklmnopqrstuvwxyz\nALI_ACCESS_KEY_ID=your_access_key_id\nALI_ACCESS_KEY_SECRET=your_access_key_secret",
          group: "security",
          condition: (p) => p.cert_mode === "dns",
        },
        {
          name: "reality_server_addr",
          type: "input",
          label: t("security_server_address", "Reality Server Address"),
          placeholder: t(
            "security_server_address_placeholder",
            "e.g. 1.2.3.4 or domain"
          ),
          group: "reality",
          condition: (p) => p.security === "reality",
        },
        {
          name: "reality_server_port",
          type: "number",
          label: t("security_server_port", "Reality Server Port"),
          min: 1,
          max: 65_535,
          placeholder: "1-65535",
          group: "reality",
          condition: (p) => p.security === "reality",
        },
        {
          name: "reality_private_key",
          type: "input",
          label: t("security_private_key", "Reality Private Key"),
          placeholder: t(
            "security_private_key_placeholder",
            "Enter private key"
          ),
          group: "reality",
          generate: {
            function: generateRealityKeyPair,
            updateFields: {
              reality_private_key: "privateKey",
              reality_public_key: "publicKey",
            } as Record<string, string>,
          },
          condition: (p) => p.security === "reality",
        },
        {
          name: "reality_public_key",
          type: "input",
          label: t("security_public_key", "Reality Public Key"),
          placeholder: t("security_public_key_placeholder", "Enter public key"),
          group: "reality",
          condition: (p) => p.security === "reality",
        },
        {
          name: "reality_short_id",
          type: "input",
          label: t("security_short_id", "Reality Short ID"),
          group: "reality",
          generate: {
            function: generateRealityShortId,
          },
          condition: (p) => p.security === "reality",
        },
      ],
    }),
    [t]
  );
}
