import { getPresetListApi } from '@/api/camera/camera-preview';
import { defineStore } from 'pinia';
import { ref } from 'vue';
import { useRequest } from 'vue-hooks-plus';

/** 当前电子围栏的store */
export const usePresetListStore = defineStore('presetListStore', () => {
  /** 当前选中的预置位 */
  const currentPresetToken = ref('');

  const { data, loading, runAsync, mutate } = useRequest(
    (cameraId: number) => {
      return getPresetListApi(cameraId);
    },
    { manual: true },
  );

  const isPresetNameExist = (name: string) => {
    return data.value?.find((x) => x.name === name);
  };

  const clear = () => {
    mutate();
    currentPresetToken.value = '';
  };

  return { data, currentPresetToken, getPresetList: runAsync, isPresetNameExist, loading, clear };
});

export default usePresetListStore;
