import {View} from '@tarojs/components'
import WidgetCard from "@/components/widgets/widget-card/index";
import WidgetTitle from "@/components/widgets/widget-title/index";
import { TComponentItem } from '@/types/agent';

interface Props {
  index: number
  editMode: boolean
  component: TComponentItem
  components: TComponentItem[]
  onSwitchChanged: (c: TComponentItem, checked: boolean)=> void
  onDelete:(c: TComponentItem)=> void
  onMove: (c: TComponentItem, direction: number) => void
  onStyleChanged: (c: TComponentItem) => void
}
export default ({
  index,
  editMode,
  component,
  components,
  onSwitchChanged,
  onDelete,
  onMove,
  onStyleChanged,
}:Props) => {
  const text = component?.data?.text ? component?.data?.text : component?.data?.placeholder
  return <>
    <View className={`${index === 0 ? "" : "mt-48"}`}>
      <WidgetCard
        enabled={component.enabled}
        onSwitchChanged={(checked)=> onSwitchChanged(component, checked)}
        index={index}
        components={components}
        editMode={editMode}
        onDelete={() => onDelete(component)}
        onMove={(direction) => onMove(component, direction)}
        onChanged={()=> {
          if(component.data?.layout === 'left'){
            onStyleChanged({...component, data: {...component.data, layout: 'center'}})
          }else{
            onStyleChanged({...component, data: {...component.data, layout: 'left'}})
          }
        }}
      >
        <WidgetTitle
          center={component?.data?.layout === "center"}
          data={component}
          editMode={editMode}
        >
          {text}
        </WidgetTitle>
      </WidgetCard>
    </View>
  </>
}