import { ref } from 'vue'
import { defineStore } from 'pinia'
import { Capacitor } from '@capacitor/core'
const useIAPStore = defineStore('iap', () => {
    const products = ref([])
    async function initialize() {
        if (Capacitor.isNativePlatform()) {
            const { store, ProductType, Platform } = CdvPurchase
            store.verbosity = CdvPurchase.LogLevel.DEBUG
            store.register([
                {
                    type: ProductType.CONSUMABLE,
                    id: '1',
                    platform: Platform.GOOGLE_PLAY
                },
                {
                    type: ProductType.CONSUMABLE,
                    id: '30',
                    platform: Platform.GOOGLE_PLAY
                },
                {
                    type: ProductType.CONSUMABLE,
                    id: '50',
                    platform: Platform.GOOGLE_PLAY
                },
                {
                    type: ProductType.CONSUMABLE,
                    id: '100',
                    platform: Platform.GOOGLE_PLAY
                },
                {
                    type: ProductType.CONSUMABLE,
                    id: '200',
                    platform: Platform.GOOGLE_PLAY
                }
            ])
            store
                .when()
                .productUpdated(product => {
                    console.log('CdvPurchase: productUpdated', product)
                    products.value = store.products
                })
                .approved(transaction => transaction.verify())
                .verified(receipt => receipt.finish())
            store.error(function (error) {
                console.error('CdvPurchase ERROR: ' + error.message)
            })
            store.initialize([Platform.GOOGLE_PLAY])
        }
    }
    initialize()
    return { initialize, products }
})
export { useIAPStore }
