import type { CornerValueSettings, FormStyleSettings } from '@/types/form-style'

export interface ActiveColors {
  buttonText?: string
  fieldText?: string
  formTitleColor?: string
  formDescriptionColor?: string
  fieldLabels?: string
  fieldPlaceholder?: string
  checkboxRadioSelection?: string
  checkboxRadioBorder?: string
  checkboxRadioLabel?: string
}

export function resolveCornerRadiusValue(
  cornerSetting: CornerValueSettings | string | undefined,
  fallbackValue?: string,
): string | undefined {
  if (!cornerSetting) {
    return fallbackValue
  }

  if (typeof cornerSetting === 'string') {
    return cornerSetting || fallbackValue
  }

  const isLinked = cornerSetting.isLinked !== false
  const linkedValue = cornerSetting.value || fallbackValue

  if (isLinked) {
    return linkedValue || fallbackValue
  }

  const topLeftValue = cornerSetting.topLeft || linkedValue || fallbackValue
  const topRightValue = cornerSetting.topRight || topLeftValue
  const bottomRightValue = cornerSetting.bottomRight || topLeftValue
  const bottomLeftValue = cornerSetting.bottomLeft || topLeftValue

  if (!topLeftValue && !topRightValue && !bottomRightValue && !bottomLeftValue) {
    return fallbackValue
  }

  return `${topLeftValue} ${topRightValue} ${bottomRightValue} ${bottomLeftValue}`
}

export function resolveActiveColors(styleSettings: FormStyleSettings): ActiveColors {
  let activeColors: ActiveColors = {}

  if (styleSettings.quickMode) {
    activeColors = {
      ...activeColors,
      fieldText: styleSettings.quickMode.fieldText,
      buttonText: styleSettings.quickMode.buttonText,
      formTitleColor: styleSettings.quickMode.formTitleText,
      fieldLabels: styleSettings.quickMode.labelFontColor,
    }
  }

  if (styleSettings.advancedMode) {
    const adv = styleSettings.advancedMode
    activeColors = {
      ...activeColors,
      formTitleColor: adv.formTitle?.fontColor || activeColors.formTitleColor,
      formDescriptionColor: adv.formDescription?.fontColor,
      fieldLabels: adv.fieldLabels?.label?.fontColor || activeColors.fieldLabels,
      fieldPlaceholder: adv.fieldPlaceholder?.fontColor,
      checkboxRadioSelection: adv.checkboxRadio?.selectionColor,
      checkboxRadioBorder: adv.checkboxRadio?.borderColor,
      checkboxRadioLabel: adv.checkboxRadio?.fontColor,
    }
  }

  return activeColors
}
