<script>
    import { onMount } from "svelte";

    import { storeMdjs, storeInspireKeywords } from "@/stores/mdjs.store";
    import { XmlConverter } from "@/libs/xml-converter/xml-converter";
    import helpers from "@/libs/helpers";

    import MultiInputField from "./elements/MultiInputField.svelte";

    export let field;
    export let list = [];

    $: isReady = $storeMdjs;

    function getValues(mdjs) {
        var keywords = mdjs ? converter.getValue(mdjs, field.name) : [{}];
        var keywordsList = [];
        var inspireKeywordsList = [];
        for (let k = 0; k < keywords.length; k++) {
            var thesaurusName = converter.getValue(keywords[k], "thesaurusName")[0];
            var keyword = converter.getValue(keywords[k], "keyword");
            if (keyword[0]) {
                if (thesaurusName && thesaurusName.toLowerCase().includes("inspire")) {
                    // var keyword = converter.getValue(keywords[k], "keyword");
                    for (let kw = 0; kw < keyword.length; kw++) {
                        let inspireKeyword = JSON.parse(JSON.stringify(keywords[k]));
                        inspireKeyword = converter.setValue(inspireKeyword, "keyword", [keyword[kw]]);
                        inspireKeywordsList.push(inspireKeyword);
                    }
                } else {
                    keywordsList.push(keywords[k]);
                }
            }
        }

        // Save keywords liste without inspireKeywords in $storeMdjs
        converter.setValue($storeMdjs, "dataKeywords", keywordsList);
        // Clean and return inspireKeywords list
        inspireKeywordsList = helpers.cleanValues(inspireKeywordsList, "keyword");
        return inspireKeywordsList;
    }
    $: {
        // Keep this 2 variables attribution to clean keywords list for each component reload
        let inspireKeywords = helpers.cleanValues($storeInspireKeywords, "keyword");
        let otherKeywords = getValues($storeMdjs);
        $storeInspireKeywords = inspireKeywords.length > 0 ? inspireKeywords : otherKeywords;
        $storeInspireKeywords.push({});
        $storeInspireKeywords = $storeInspireKeywords;
    }

    function getChildren(data, fieldName) {
        return converter.getValue(data, fieldName)[0] || "";
    }

    const converter = new XmlConverter();

    onMount(() => {});

    function onRemove(id) {
        $storeInspireKeywords.splice(id, 1);
        $storeInspireKeywords = $storeInspireKeywords;
    }
    function onSave(fieldName, fieldValue, id) {
        if (fieldValue) {
            let inspireKeyword = converter.setValue({}, fieldName, [fieldValue]);
            inspireKeyword = converter.setValue(inspireKeyword, "keywordType", ["Theme"]);
            inspireKeyword = converter.setValue(inspireKeyword, "thesaurusName", [
                "GEMET - INSPIRE themes, version 1.0",
            ]);
            inspireKeyword = converter.setValue(inspireKeyword, "thesaurusDatePublication", ["2008-06-01"]);
            $storeInspireKeywords[id] = inspireKeyword;
        } else {
            $storeInspireKeywords[id] = {};
        }
        $storeInspireKeywords = helpers.cleanValues($storeInspireKeywords, "keyword", true);
    }
</script>

{#if isReady}
    <MultiInputField
        field={field.children.keyword}
        {list}
        values={$storeInspireKeywords}
        {onSave}
        {onRemove}
        {getChildren}
        strong={true}
    />
{/if}

<style>
</style>
