import {defineStore} from 'pinia'
import {config} from '@/config'
import {useNotificationStore} from "@/store/notification.js";
import {useStageStore} from "@/store/stage.js";
import {usePropertySetsStore} from "@/store/propertySets.js";
import performRequest from "@/backend.js";

export const useCountryStore = defineStore('country', {
    state() {
        return {
            countries: null,
            loading: false,
            query: null,
            selectedCountryId: null,
            selectedPeriodId: null,
            countryDetail: null,
        }
    },
    getters: {
        getCountries(state) {
            return state.countries;
        },
        getSelectedCountry(state) {
            return state.countryDetail;
        }

    },
    actions: {
        async setProperty(property) {
            if(this.getSelectedCountry === null) return;

            const prop = this.getSelectedCountry.properties.find(p => p.external_mapping_id === property.id);

            if((prop ?? null) === null) return;

            const url = `${config.backendUrl}/properties/country/${this.getSelectedCountry.iso_code}/${property.id}`;
            const body = { value: String(property.value)};

            await performRequest(this,'PUT', url, body, false);

            prop.draft_value = property.reset ? null : property.value;

            const stage = useStageStore();
            await stage.checkStagedChanges();
        },
        async selectPeriod(periodId) {
            this.loading = true;
            this.countryDetail = null;
            this.selectedPeriodId = periodId;
            await this.loadCountryDetail();
            this.loading = false;
        },
        async selectCountry(countryId) {
            this.selectedCountryId = countryId;
            await this.loadCountryDetail();
        },
        async loadCountryDetail() {
            if(this.selectedCountryId == null) return;
            this.loading = true;

            const params = new URLSearchParams();
            if(this.selectedPeriodId !== null)
                params.append('property_set', this.selectedPeriodId);

            const url = `${config.backendUrl}/countries/${this.selectedCountryId}/${params.size === 0 ? '' : '?'}${params.toString()}`;
            const resp = await performRequest(this,'GET', url, null);
            this.countryDetail = resp.data;
            this.loading = false;
        },
        async setQuery(query) {
            this.query = query;
            await this.fetchCountryList();
        },
        async reload() {
            const stage = useStageStore();
            await stage.checkStagedChanges();
        },
        async fetchCountryList() {
            const params = new URLSearchParams();
            if (this.query !== null)
                params.append('filter', this.query);
            const url = `${config.backendUrl}/countries/all/${params.size === 0 ? '' : '?'}${params.toString()}`;
            const resp = await performRequest(this, 'GET', url, null);
            this.countries = resp.data;
        }
    },

});