import test from 'node:test'
import assert from 'node:assert/strict'

import {
  hasCloudtentaclesCredentialContextChanged,
  isPlainObject,
  pickFirstNonEmpty,
  resolveCloudtentaclesAdminContext,
  resolveKuaishouEticketAdminContext,
} from './context.js'

test('pickFirstNonEmpty returns first trimmed non-empty string value', () => {
  assert.equal(pickFirstNonEmpty(['', '  ', ' value ', 'fallback']), 'value')
  assert.equal(pickFirstNonEmpty([null, undefined, 0, '']), '')
})

test('isPlainObject only accepts non-array objects', () => {
  assert.equal(isPlainObject({ a: 1 }), true)
  assert.equal(isPlainObject([]), false)
  assert.equal(isPlainObject(null), false)
  assert.equal(isPlainObject('x'), false)
})

test('resolveKuaishouEticketAdminContext prefers explicit payload values', () => {
  const context = resolveKuaishouEticketAdminContext(
    {
      baseUrl: ' https://override.example.com ',
      shopId: ' shop-2 ',
      cookie: ' cookie-2 ',
    },
    {
      savedSource: { baseUrl: 'https://saved.example.com' },
      findShopConfig(requestedShopId) {
        assert.equal(requestedShopId, 'shop-2')
        return { shopId: 'shop-2', cookie: 'saved-cookie-2' }
      },
      getFirstAvailableShop() {
        throw new Error('should not use first available shop')
      },
    },
  )

  assert.deepEqual(context, {
    baseUrl: 'https://override.example.com',
    cookie: 'cookie-2',
    shopId: 'shop-2',
    shop: { shopId: 'shop-2', cookie: 'saved-cookie-2' },
  })
})

test('resolveKuaishouEticketAdminContext falls back to first available shop', () => {
  const context = resolveKuaishouEticketAdminContext(
    {},
    {
      savedSource: { baseUrl: ' https://saved.example.com ' },
      getFirstAvailableShop() {
        return { shopId: 'shop-1', cookie: 'cookie-1' }
      },
    },
  )

  assert.deepEqual(context, {
    baseUrl: 'https://saved.example.com',
    cookie: 'cookie-1',
    shopId: 'shop-1',
    shop: { shopId: 'shop-1', cookie: 'cookie-1' },
  })
})

test('resolveCloudtentaclesAdminContext merges payload source and persisted session', () => {
  const context = resolveCloudtentaclesAdminContext(
    {
      token: ' token-1 ',
      deviceType: 3,
    },
    {
      savedSource: {
        baseUrl: ' https://saved.example.com ',
        deviceId: ' device-saved ',
        deviceType: 1,
      },
      persistedSession: {
        token: 'token-persisted',
        deviceId: 'device-persisted',
        deviceType: 2,
      },
    },
  )

  assert.deepEqual(context, {
    sourceKey: 'default',
    baseUrl: 'https://saved.example.com',
    token: 'token-1',
    deviceId: 'device-saved',
    deviceType: 3,
  })
})

test('hasCloudtentaclesCredentialContextChanged detects normalized credential changes', () => {
  assert.equal(
    hasCloudtentaclesCredentialContextChanged(
      { baseUrl: ' https://a ', username: 'u1', phone: '13812345678', deviceId: 'd1', deviceType: 1 },
      { baseUrl: 'https://a', username: 'u1', phone: '13812345678', deviceId: 'd1', deviceType: 1 },
    ),
    false,
  )

  assert.equal(
    hasCloudtentaclesCredentialContextChanged(
      { baseUrl: 'https://a', username: 'u1', phone: '13812345678', deviceId: 'd1', deviceType: 1 },
      { baseUrl: 'https://b', username: 'u1', phone: '13812345678', deviceId: 'd1', deviceType: 1 },
    ),
    true,
  )
})
