import { getMemberServiceAgreement } from '@/api/member'

const defaultAgreement = () => ({
  enabled: false,
  registrationOpen: true,
  requireAgreementOnLogin: false,
  message: '',
  agreementTitle: '',
  versionLabel: '',
  content: '',
  checkboxLabel: '我已阅读并同意相关服务协议'
})

/** 拉取服务协议配置（注册/登录页共用） */
export async function loadServiceAgreement() {
  try {
    const res = await getMemberServiceAgreement()
    const data = res.data || {}
    return {
      enabled: !!data.enabled,
      registrationOpen: data.registrationOpen !== false,
      requireAgreementOnLogin: !!data.requireAgreementOnLogin,
      message: data.message || '',
      agreementTitle: data.agreementTitle || '',
      versionLabel: data.versionLabel || '',
      content: data.content || '',
      checkboxLabel: data.checkboxLabel || defaultAgreement().checkboxLabel
    }
  } catch (e) {
    return defaultAgreement()
  }
}
