<script lang="ts" setup>
import { computed } from "vue";
import { Message, type SwitchInstance } from "@arco-design/web-vue";
import type { BaseResponse } from "@chatgpt-plus/packages/type";

type OriginProps = SwitchInstance["$props"];

interface Props extends /* @vue-ignore */ OriginProps {
  modelValue: boolean | string | number;
  api: (params?: any) => Promise<BaseResponse<any>>;
  onSuccess?: (res?: any) => void;
}

const props = defineProps<Props>();

const emits = defineEmits(["update:modelValue"]);

const _value = computed({
  get: () => props.modelValue,
  set: (v) => {
    emits("update:modelValue", v);
  },
});

const onBeforeChange = async (params) => {
  try {
    const res = await props.api({ ...params, value: !_value.value });
    Message.success("操作成功");
    props?.onSuccess?.(res);
    return true;
  } catch (err) {
    console.log(err);
    return false;
  }
};
</script>
<template>
  <a-switch v-bind="{ ...props, ...$attrs }" v-model="_value" :before-change="onBeforeChange" />
</template>
