import {defineStore} from "pinia";
import {fetchSettings} from "@/utils/ftch.js";

export const useSettingsStore = defineStore('settings', {
    state: () => ({
        app_enabled: true,
        app_debug: false,
        product_interaction_mode: 'browser',
        manager_username: null,
        app_name: 'OpenCart Telegram магазин',
        app_icon: '',
        night_auto: true,
        ya_metrika_enabled: false,
        feature_coupons: false,
        feature_vouchers: false,
        show_category_products_button: true,
        currency_code: null,
        haptic_enabled: true,
        theme: {
            light: 'light', dark: 'dark', variables: {
                '--product_list_title_max_lines': 2,
            }
        },
        texts: {
            text_no_more_products: 'Нет товаров',
            text_empty_cart: 'Корзина пуста',
            text_order_created_success: 'Заказ успешно оформлен.',
            text_manager_button: '💬 Связаться с менеджером',
        },
        mainpage_blocks: [],
        is_privacy_consented: true,
        privacy_policy_link: false,
        image_aspect_ratio: '1:1',
    }),

    actions: {
        async load() {
            console.log('Load settings');
            const settings = await fetchSettings();
            this.app_name = settings.app_name;
            this.app_icon = settings.app_icon;
            this.theme.light = settings.theme_light;
            this.theme.dark = settings.theme_dark;
            this.ya_metrika_enabled = settings.ya_metrika_enabled;
            this.app_enabled = settings.app_enabled;
            this.app_debug = settings.app_debug;
            this.product_interaction_mode = settings.product_interaction_mode ?? 'browser';
            this.manager_username = settings.manager_username;
            this.feature_coupons = settings.feature_coupons;
            this.feature_vouchers = settings.feature_vouchers;
            this.show_category_products_button = settings.show_category_products_button ?? true;
            this.currency_code = settings.currency_code;
            this.texts = settings.texts;
            this.mainpage_blocks = settings.mainpage_blocks;
            this.privacy_policy_link = settings.privacy_policy_link;
            this.image_aspect_ratio = settings.image_aspect_ratio;
            this.haptic_enabled = settings.haptic_enabled ?? true;
        }
    }
});
