import { persisted } from 'svelte-persisted-store';
import { writable } from 'svelte/store';
import type { Writable } from 'svelte/store';

import type { key_val } from '$lib/stores/ae_stores';
import { offset } from '@floating-ui/dom';

// Set the version for the app data. Changing this should force a notification and ask the user to clear and reload the page.
const ver = '2024-08-21_1646';
const ver_idb = '2024-08-21_1645';

/* *** BEGIN *** Initialize idaa_local_data_struct */
// Longer-term app data. This should be stored to *local* storage.
// Updated 2024-03-06
const idaa_local_data_struct: key_val = {
    ver: ver,
    ver_idb: ver_idb,

    // Shared
    name: 'Aether - IDAA (SvelteKit 2.x Svelte 4.x)',
    title: `OSIT's Æ IDAA`, // - Dev SvelteKit`, // &AElig;

    novi_uuid: null,
    novi_email: null,
    novi_full_name: null,
    novi_admin_li: ['2b078deb-b4e7-4203-99da-9f7cd62159a5'],
    novi_trusted_li: [
        'c9ea07b5-06b0-4a43-a2d0-8d06558c8a82',
        '58db22ee-4b0a-49a7-9f34-53d2ba85a84b'
    ],
    novi_jitsi_mod_li: ['5724aad7-6d89-47e7-8943-966fd22911bd'],

    novi_archives_base_url: 'https://www.idaa.org/idaa-archives',
    novi_bb_base_url: 'https://www.idaa.org/idaa-bulletin-board',
    novi_meetings_base_url: 'https://www.idaa.org/idaa-meetings',

    ds: {},

    idaa_cfg_json: {},

    // all, disabled, enabled
    qry__enabled: 'enabled',
    // all, hidden, not_hidden
    qry__hidden: 'not_hidden',
    qry__limit: 20,
    qry__offset: 0,

    archives: {
        enabled: 'enabled', // all, disabled, enabled
        hidden: 'not_hidden', // all, hidden, not_hidden
        limit: 150,
        offset: 0,

        edit_kv: {}, // Used to track which archive objects are being edited
        edit__archive_obj: null,
        edit__archive_content_obj: null

        // qry__order_by: 'updated_on',  //  For the IDB index query
        // qry__order_by_li: {'priority': 'DESC', 'sort': 'DESC', 'updated_on': 'DESC', 'created_on': 'DESC', 'name': 'ASC'}, // For the SQL query
    },

    bb: {
        enabled: 'enabled', // all, disabled, enabled
        hidden: 'not_hidden', // all, hidden, not_hidden
        limit: 50,
        offset: 0,

        edit_kv: {}, // Used to track which post objects are being edited
        edit__post_obj: null,
        edit__post_comment_obj: null,

        show_list__post_obj_li: true,

        qry__enabled: 'enabled', // all, disabled, enabled
        qry__hidden: 'not_hidden', // all, hidden, not_hidden
        qry__limit: 25,
        qry__offset: 0,
        qry__order_by: 'updated_on', //  For the IDB index query
        qry__order_by_li: { updated_on: 'DESC', created_on: 'DESC' } // For the SQL query
    },

    recovery_meetings: {
        edit_kv: {}, // Used to track which event objects are being edited
        edit__event_obj: null,

        qry__enabled: 'enabled', // all, disabled, enabled
        qry__hidden: 'not_hidden', // all, hidden, not_hidden
        qry__limit: 150,
        qry__order_by: 'updated_on', //  For the IDB index query; name, updated_on/created_on
        qry__order_by_li: {
            priority: 'DESC',
            sort: 'DESC',
            updated_on: 'DESC',
            created_on: 'DESC',
            name: 'ASC'
        }, // For the SQL query
        qry__offset: 0,

        qry__fulltext_str: null,
        qry__physical: null,
        qry__type: null,
        qry__virtual: null
    }
};
// console.log(`AE Stores - App IDAA Local Storage Data:`, idaa_local_data_struct);

// This works, but does not uses local storage:
// export let ae_loc = writable(idaa_local_data_struct);

// This works and uses *local* storage:
export const idaa_loc: Writable<key_val> = persisted('ae_idaa_loc', idaa_local_data_struct);
// console.log(`AE Stores - App Local Storage Data:`, get(ae_loc));

/* *** BEGIN *** Initialize idaa_session_data_struct */
// Temporary app data. This should be stored to session storage.
// Updated 2024-03-06
const idaa_session_data_struct: key_val = {
    ver: ver,
    ver_idb: ver_idb,
    log_lvl: 1,

    archives: {
        qry__status: null,
        show__modal_edit__archive_id: null,
        show__modal_view__archive_id: null,
        show__modal_edit__archive_content_id: null,
        show__modal_view__archive_content_id: null,
        obj_changed: false // Used to track if the archive object has been changed in the edit view
    },

    bb: {
        qry__status: null,

        edit__post_obj: null,

        show__inline_edit__post_obj: null,
        show__modal_edit__post_id: null,
        show__modal_view__post_id: null,
        obj_changed: false // Used to track if the post object has been changed in the edit view
        // edit_kv: {}, // Used to track which post objects are being edited
    },

    recovery_meetings: {
        qry__status: null,
        qry__fulltext_str: null,

        edit__event_obj: null,

        status_qry__last_request_str: null,

        show__modal_edit: false,
        show__modal_view: false,
        show__modal_edit__event_id: null,
        show__modal_view__event_id: null,
        obj_changed: false, // Used to track if the event object has been changed in the edit view

        attend_platform: null // 'Zoom', 'Google Meet', 'Microsoft Teams', etc.
    }
};
// console.log(`AE Stores - App IDAA Session Storage Data:`, idaa_session_data_struct);
export const idaa_sess = writable(idaa_session_data_struct);

/* *** BEGIN *** Initialize idaa_slct and idaa_trig */
/* The slct and slct_trigger variable should not be stored in local storage. Only use session storage because browser tabs can be open to different idaa, badges, exhibits, etc. */

// Intended for temporary session storage.
// Updated 2024-03-06
const idaa_slct_obj_template: key_val = {
    // Top level
    event_id: null,
    event_obj: {},
    event_obj_li: [],

    archive_id: null,
    archive_obj: {},
    archive_obj_li: [],

    archive_content_id: null,
    archive_content_obj: {},
    archive_content_obj_li: [],

    post_id: null,
    post_obj: {},
    post_obj_li: [],

    post_comment_id: null,
    post_comment_obj: {},
    post_comment_obj_li: []
};
// console.log(`AE Stores - Selected IDAA Objects:`, idaa_slct_obj_template);

// This works, and uses *session* (not local) storage:
export const idaa_slct = writable(idaa_slct_obj_template);

// This works and uses *local* storage:
// export let idaa_slct: Writable<key_val> = persisted('ae_idaa_slct', idaa_slct_obj_template);

/* *** BEGIN *** Initialize idaa_trig */
// Intended for temporary session storage.
// Updated 2024-11-19
const idaa_trig_template: key_val = {
    archive_id: false,
    archive_content_li: false,
    event_id: false,
    post_id: false
};
export const idaa_trig: any = writable(idaa_trig_template);
// console.log(`AE IDAA Stores - IDAA Trigger:`, idaa_trig);

/* *** BEGIN *** Initialize idaa_prom */
// Intended for temporary session storage.
// Updated 2024-11-19
const idaa_prom_template: key_val = {
    archive_id: false,
    archive_content_li: false,
    event_id: false,
    post_id: false
};
export const idaa_prom: any = writable(idaa_prom_template);
// console.log(`AE IDAA Stores - IDAA Trigger:`, idaa_prom);
