import {
  maskPhone as maskPhoneValue,
  maskSecret as maskSecretValue,
} from "../../../utils/masking.js";

type JsonObject = Record<string, any>

export function maskSecret(value: unknown) {
  return maskSecretValue(value);
}

export function maskPhone(value: unknown) {
  return maskPhoneValue(value, { maskShort: false });
}

export function mapAdminKuaishouEticketShopItem(item: JsonObject = {}) {
  return {
    shopId: String(item.shopId || "").trim(),
    kshopName: String(item.kshopName || "").trim(),
    cookie: String(item.cookie || "").trim(),
    cookieMasked: maskSecret(item.cookie),
    hasCookie: Boolean(String(item.cookie || "").trim()),
    userAvatar: String(item.userAvatar || "").trim(),
    enabled: item.enabled !== false,
  };
}

export function mapAdminKuaishouEticketSourceConfig(config: JsonObject = {}, shops: JsonObject[] = []) {
  return {
    enabled: config.enabled !== false,
    baseUrl: String(config.baseUrl || "").trim(),
    shops: shops.map((item) => mapAdminKuaishouEticketShopItem(item)),
  };
}

export function mapAdminCloudtentaclesSourceConfig(config: JsonObject = {}) {
  return {
    key: String(config.key || "").trim(),
    label: String(config.label || "").trim(),
    enabled: config.enabled !== false,
    baseUrl: String(config.baseUrl || "").trim(),
    username: String(config.username || "").trim(),
    password: String(config.password || "").trim(),
    phone: String(config.phone || "").trim(),
    deviceId: String(config.deviceId || "-").trim() || "-",
    deviceType: Number(config.deviceType || 0),
  };
}

export function mapAdminCloudtentaclesSession(session: JsonObject = {}) {
  return {
    token: String(session.token || "").trim(),
    tokenMasked: maskSecret(session.token),
    baseUrl: String(session.baseUrl || "").trim(),
    username: String(session.username || "").trim(),
    phoneMasked: maskPhone(session.phone),
    loggedInAt: String(session.loggedInAt || "").trim(),
    deviceId: String(session.deviceId || "-").trim() || "-",
    deviceType: Number(session.deviceType || 0),
    hasToken: Boolean(String(session.token || "").trim()),
  };
}

export function mapAdminKuaishouCloudFulfillmentItem(item: JsonObject = {}) {
  return {
    id: String(item.id || "").trim(),
    enabled: item.enabled !== false,
    priority: Number(item.priority || 100) || 100,
    provider: String(item.provider || "91kaquan").trim() || "91kaquan",
    platform: String(item.platform || "kuaishou").trim() || "kuaishou",
    shopId: String(item.shopId || "").trim(),
    internalSkuCode: String(item.internalSkuCode || "").trim(),
    internalSkuName: String(item.internalSkuName || "").trim(),
    externalSkuCode: String(item.externalSkuCode || "").trim(),
    externalItemId: String(item.externalItemId || "").trim(),
    externalSkuName: String(item.externalSkuName || "").trim(),
    resolvedSkuName: String(item.resolvedSkuName || "").trim(),
    cloudSourceKey:
      String(item.cloudSourceKey || "default").trim() || "default",
    cloudSkuId: Number(item.cloudSkuId || 0) || 0,
    cloudSkuName: String(item.cloudSkuName || "").trim(),
    vnKey: String(item.vnKey || "").trim(),
    autoBuyEnabled: item.autoBuyEnabled !== false,
    minAssetReserve: Number(item.minAssetReserve || 0) || 0,
    autoReturnNumberAfterDispatch: item.autoReturnNumberAfterDispatch === true,
    autoConsumeAfterDispatch: item.autoConsumeAfterDispatch === true,
    kuaishouConsumeShopId: String(item.kuaishouConsumeShopId || "").trim(),
    kuaishouConsumeShopName: String(item.kuaishouConsumeShopName || "").trim(),
    notes: String(item.notes || "").trim(),
  };
}

export function mapAdminKuaishouCloudFulfillmentSource(config: JsonObject = {}) {
  return {
    enabled: config.enabled !== false,
    items: (Array.isArray(config.items) ? config.items : []).map((item) =>
      mapAdminKuaishouCloudFulfillmentItem(item)
    ),
  };
}
