import {
  PadMode,
  RandomMode,
  Rollback,
  SectionName,
  TimeUnit,
} from '@/pages/sys/sn-config/contant.ts'
import Colls from '@/common/utils/colls.ts'
import { nanoid } from 'nanoid'

export function descGuDingSection(section: SnConfigTypes.FixedSectionConfig) {
  return `${SectionName.GuDing}：${section.value}`
}

export function descShiJianSection(section: SnConfigTypes.TimeSectionConfig) {
  return `${SectionName.ShiJian}：${section.timestamp ? (section.unit != null ? TimeUnit[section.unit!] + '（时间戳）' : '（时间戳）') : section.pattern}`
}

export function descZhiZengSection(section: SnConfigTypes.IncSectionConfig) {
  if (section.padMode === 'Wu') {
    return `${SectionName.ZiZeng}：初始值为：${section.initialVal}，步长为：${section.step}，${PadMode[section.padMode]}字符，${Rollback[section.rollback] ?? Rollback.Wu}，${section.allowOverflow ? '允许溢出' : '不允许溢出'}`
  } else {
    return `${SectionName.ZiZeng}：初始值为：${section.initialVal}，步长为：${section.step}，${PadMode[section.padMode]}（${section.padLen} 个字符：${section.padVal}），${Rollback[section.rollback] ?? Rollback.Wu}，${section.allowOverflow ? '允许溢出' : '不允许溢出'}`
  }
}

export function descSuiJiSection(section: SnConfigTypes.RandomSectionConfig) {
  switch (section.randomMode) {
    case 'Snowflake':
      return `${SectionName.SuiJi}：${RandomMode[section.randomMode]}（工作节点：${section.workerId ?? ''}，数据中心：${section.datacenterId ?? ''}）`
    case 'NanoId':
      return `${SectionName.SuiJi}：${RandomMode[section.randomMode]}（长度：${section.nanoIdSize ?? ''}）`
    case 'UUID':
      return `${SectionName.SuiJi}：${RandomMode[section.randomMode]}`
  }
}

export function descConfig(sections: SnConfigTypes.Section[]) {
  if (Colls.isEmpty(sections)) {
    return []
  }
  return sections.map(it => descSection(it))
}

export function descSection(section: SnConfigTypes.Section) {
  switch (section.sectionName) {
    case 'GuDing':
      return descGuDingSection(section as SnConfigTypes.FixedSectionConfig)
    case 'ShiJian':
      return descShiJianSection(section as SnConfigTypes.TimeSectionConfig)
    case 'ZiZeng':
      return descZhiZengSection(section as SnConfigTypes.IncSectionConfig)
    case 'SuiJi':
      return descSuiJiSection(section as SnConfigTypes.RandomSectionConfig)
    default:
      throw new Error(`不支持的配置项: ${section.sectionName}`)
  }
}

export function createSection(sectionName: SnConfigTypes.SectionType): SnConfigTypes.Section {
  switch (sectionName) {
    case 'GuDing':
      return {
        sectionName,
        value: '',
      }
    case 'ShiJian':
      return {
        sectionName,
        timestamp: false,
        unit: null,
        pattern: 'yyyyMMdd',
      }
    case 'ZiZeng':
      return {
        sectionName,
        code: nanoid(),
        padMode: 'Wu',
        padVal: '',
        padLen: null,
        rollback: 'Wu',
        allowOverflow: true,
        initialVal: 1,
        step: 1,
      }
    case 'SuiJi':
      return {
        sectionName,
        randomMode: 'UUID',
        workerId: null,
        datacenterId: null,
        nanoIdSize: null,
      }
    default:
      throw new Error(`不支持的配置项: ${sectionName}`)
  }
}

export default {
  descGuDingSection,
  descShiJianSection,
  descZhiZengSection,
  descSuiJiSection,
  descConfig,
  descSection,
  createSection,
}
