import { create } from "zustand";

import { TGetMyVoicesParams, TVoiceItem, } from '@/types/voice'

import {
  getVoices as _getVoices,
  cloneVoice as _cloneVoice,
  
} from "@/service/voice";
import { isSuccess } from "@/utils";


type TVoiceRequestParam = Omit<TGetMyVoicesParams, 'entId'>
export interface StorageStoreState {
  // 初始状态
  entId: number|string
  voices: TVoiceItem[];
  myVoices: TVoiceItem[];
  setEntId: (entId: number|string)=> void
  getVoices: (params?: TVoiceRequestParam) => Promise<boolean>;
  cloneVoice: (params: {
    sourceUrl: string // 源语音地址 ,
    voiceText?: string // 录音文案
  }) => Promise<void>;
}

export const useVoiceStore = create<StorageStoreState>((set, get) => ({
  entId: 0,
  voices: [],
  myVoices: [],
  pagination: {
    pageIndex: 1,
    pageSize: 10,
    totalCount: 0,
    searchKey: '',
    extData: null,
  },
  setEntId: (entId)=> {
    set({entId: entId})
  },
  // 获取第一页全部声音
  getVoices: async (params = {}) => {
    const requestParams = {
      pageIndex: 1,
      pageSize: 10,
      entId: get().entId, ...params };
    const response = await _getVoices(requestParams)

    // 更新状态
    set({
      voices: response.data.data,
    });
    return isSuccess(response.status)
  },
  cloneVoice: async (params) => {
    await _cloneVoice(params)
  }
}));
