import {get, writable, derived } from 'svelte/store';
import lodash from 'lodash';
import { XmlConverter } from '@/libs/xml-converter/xml-converter'
import helpers from '@/libs/helpers';


    // this.mdjs = this.LodashService.lodash.merge(
    //         this.XmlConverterService.xml2js(this.defaultXml),
    //         this.XmlConverterService.xml2js(this.xml)
    //     );
    // helpers.getFile("config/models/empty.xml", 'xml').then((response) => {
    //                     storeMdjs.setXml(response.data);
    //                     storeMdjs.setXml(newFile);
    //                     update(() => 'xml');
    //                 });


const createMdjs = function(mdjs) {
    const { subscribe, set, update } = writable(mdjs);
    return {
        subscribe,
        set,
        setMdjs: (newMdjs) => update(mdjs => newMdjs),
        setXml: (xml) => {
            const converter = new XmlConverter();

            helpers.getFile("config/models/empty.xml", 'xml').then((response) => {

                const mdjsDefault = converter.xml2js(response.data, {
                    compact: true,
                    spaces: 4,
                    alwaysArray: true,
                    trim: true
                });
                const mdjsXml = converter.xml2js(xml, {
                    compact: true,
                    spaces: 4,
                    alwaysArray: true,
                    trim: true
                });

                const mdjs = lodash.merge(mdjsDefault, mdjsXml);
                set(mdjs);
            });


            // const mdjs = converter.xml2js(xml, {
            //     compact: true,
            //     spaces: 4,
            //     alwaysArray: true,
            //     trim: true
            // });
            // set(mdjs);
        },
        reset: () => set(mdjs)
    };
};
const storeMdjs = createMdjs();

const createThemeKeywords = function(themeKeywords) {
    const { subscribe, set, update } = writable(themeKeywords);
    return {
        subscribe,
        set,
        setThemeKeywords: (newThemeKeywords) => update(themeKeywords => newThemeKeywords),
        pushList: (keyword) => {
            themeKeywords.push(keyword);
            set(themeKeywords);
        },
        reset: () => set(themeKeywords)
    };
};
const storeThemeKeywords = createThemeKeywords([]);

const createInspireKeywords = function(inspireKeywords) {
    const { subscribe, set, update } = writable(inspireKeywords);
    return {
        subscribe,
        set,
        setInspireKeywords: (newInspireKeywords) => update(inspireKeywords => newInspireKeywords),
        pushList: (keyword) => {
            inspireKeywords.push(keyword);
            set(inspireKeywords);
        },
        reset: () => set(inspireKeywords)
    };
};
const storeInspireKeywords = createInspireKeywords([]);

export {
    storeMdjs,
    storeThemeKeywords,
    storeInspireKeywords
};