import type { JsonObject } from '../../../../types/json.js'
import {
  maskPhone as maskPhoneValue,
  maskSecret as maskSecretValue,
} from '../../../../utils/masking.js'
import {
  normalizeCloudtentaclesDeviceId,
  normalizeCloudtentaclesDeviceType,
} from '../../../platforms/cloudtentacles/defaults.js'

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

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

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: normalizeCloudtentaclesDeviceId(config.deviceId),
    deviceType: normalizeCloudtentaclesDeviceType(config.deviceType),
  }
}

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: normalizeCloudtentaclesDeviceId(session.deviceId),
    deviceType: normalizeCloudtentaclesDeviceType(session.deviceType),
    hasToken: Boolean(String(session.token || '').trim()),
  }
}
