// @ts-ignore
import {defineStore} from 'pinia';
// @ts-ignore
import {useProvinceStore} from "../province";
import {useCitiesStore} from "../cities";

export interface Filter {
    price_min: string,
    price_max: string,
    image: boolean,
    ai: boolean,
    special: boolean,
    exchange: boolean,
    requested: boolean,
    category: string,
    attributes: string[],
    province: string,
    city: string,
    zone: string,
    mahale: string,
    search: string,
    type: string,
}

export const useFilterStore = defineStore('filter', {
    state: () => (<Filter>{
        price_min: "",
        price_max: "",
        image: false,
        ai: false,
        special: false,
        exchange: false,
        requested: false,
        category: "",
        attributes: [],
        province: "",
        city: "",
        zone: "",
        mahale: "",
        search: "",
        type: "all",
    }),

    actions: {
        async setFilter(filter: Filter) {
            const province = useProvinceStore();
            const city = useCitiesStore();

            if(province.province !== "") {
                // @ts-ignore
                filter.province = province.province.id;
            }

            if(city.cities.length > 0) {
                // @ts-ignore
                filter.city = city.cities.map(item => item.id).join(',');
            }

            // @ts-ignore
            this.filter = filter;
        },
        async setItemFilter(delimiter: String,value: any) {
            const province = useProvinceStore();
            const city = useCitiesStore();

            if(province.province !== "") {
                // @ts-ignore
                this.province = province.province.id;
            }

            if(city.cities.length > 0) {
                // @ts-ignore
                this.city = city.cities.map(item => item.id).join(',');
            }

            // @ts-ignore
            eval(`this.${delimiter} = value`);
        },
        resetFilter() {
    // ✅ ریست واقعی: هیچ چیزی از province/cities دوباره نریز
    this.price_min = "";
    this.price_max = "";
    this.image = false;
    this.ai = false;
    this.special = false;
    this.exchange = false;
    this.requested = false;
    this.category = "";
    this.attributes = [];
    this.province = "";
    this.city = "";
    this.zone = "";
    this.mahale = "";
    this.search = "";
    this.type = "all";
  },
    },

    getters: {
        getFilter: (state: Filter) => state
    }
});