
export const useNumberRange = (resourceType) => {
    const supabase = useSupabaseClient()

    const dataStore = useDataStore()
    const profileStore = useProfileStore()

    const numberRanges = profileStore.ownTenant.numberRanges

    const numberRange = numberRanges[resourceType]

    const useNextNumber = async () => {

        let nextNumber = numberRange.nextNumber

        let newNumberRanges = numberRanges

        newNumberRanges[resourceType].nextNumber += 1

        const {data,error} = await supabase
            .from("tenants")
            .update({numberRanges: newNumberRanges})
            .eq('id',profileStore.currentTenant)


        await profileStore.fetchOwnTenant()

        return (numberRange.prefix ? numberRange.prefix : "") + nextNumber + (numberRange.suffix ? numberRange.suffix : "")
    }

    return { useNextNumber}
}

/*export const useNumberRange = (resourceType) => {
    const supabase = useSupabaseClient()

    const {numberRanges} = storeToRefs(useDataStore())
    const {fetchNumberRanges} = useDataStore()

    const numberRange = numberRanges.value.find(range => range.resourceType === resourceType)




    const useNextNumber = async () => {

        let nextNumber = numberRange.nextNumber

        const {data,error} = await supabase
            .from("numberranges")
            .update({nextNumber: nextNumber + 1})
            .eq('id',numberRange.id)

        fetchNumberRanges()

        return (numberRange.prefix ? numberRange.prefix : "") + nextNumber + (numberRange.suffix ? numberRange.suffix : "")

    }

    return { useNextNumber}
}*/