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

import {
  buildAdminAgisoMessagingSavePayload,
  buildAdminAgisoShopConfigUpdate,
  normalizeAdminCloudtentaclesSourceConfigPayload,
} from './writes.js'

test('buildAdminAgisoShopConfigUpdate merges item fields and drops explicit empty templates', () => {
  assert.deepEqual(
    buildAdminAgisoShopConfigUpdate(
      {
        shopId: ' shop-1 ',
        shopName: '   ',
        accessToken: ' new-token ',
        messageTemplate: '   ',
        autoDeliveryMessageTemplate: ' done ',
        enabled: false,
      },
      {
        shopName: '旧店铺',
        accessToken: 'old-token',
        messageTemplate: 'old-template',
        autoDeliveryMessageTemplate: 'old-auto',
        appSecret: 'keep-secret',
      },
    ),
    {
      shopId: 'shop-1',
      config: {
        accessToken: 'new-token',
        autoDeliveryMessageTemplate: 'done',
        appSecret: 'keep-secret',
        enabled: false,
      },
    },
  )
})

test('buildAdminAgisoMessagingSavePayload updates defaults and keeps only shops with resulting access token', () => {
  assert.deepEqual(
    buildAdminAgisoMessagingSavePayload(
      {
        defaults: {
          messageTemplate: ' next\\nline ',
          autoDeliveryMessageTemplate: '   ',
        },
        shops: [
          {
            shopId: ' shop-1 ',
            shopName: '店铺一',
            accessToken: '',
            enabled: true,
          },
          {
            shopId: 'shop-2',
            shopName: ' 店铺二 ',
            accessToken: ' token-2 ',
            messageTemplate: ' hi ',
            apiVersion: ' v2 ',
          },
          {
            shopId: '',
            accessToken: 'skip',
          },
        ],
      },
      {
        currentDefaults: {
          messageTemplate: 'old',
          autoDeliveryMessageTemplate: 'old-auto',
        },
        currentMap: {
          'shop-1': {
            accessToken: 'token-1',
            autoDeliveryMessageTemplate: 'keep-auto',
          },
        },
      },
    ),
    {
      defaults: {
        messageTemplate: 'next\nline',
      },
      shops: {
        'shop-1': {
          accessToken: 'token-1',
          autoDeliveryMessageTemplate: 'keep-auto',
          enabled: true,
          shopName: '店铺一',
        },
        'shop-2': {
          accessToken: 'token-2',
          apiVersion: 'v2',
          messageTemplate: 'hi',
          shopName: '店铺二',
        },
      },
    },
  )
})

test('normalizeAdminCloudtentaclesSourceConfigPayload applies defaults', () => {
  assert.deepEqual(
    normalizeAdminCloudtentaclesSourceConfigPayload({
      enabled: false,
      baseUrl: ' ',
      username: ' user ',
      password: ' pass ',
      phone: ' 13800138000 ',
      deviceId: ' ',
      deviceType: '2',
    }),
    {
      key: 'default',
      label: '',
      enabled: false,
      baseUrl: 'https://123.207.217.176',
      username: 'user',
      password: 'pass',
      phone: '13800138000',
      deviceId: '-',
      deviceType: 2,
    },
  )
})
