/* Essentials YOOtheme Pro 2.2.16 build 0813.1608; ZOOlanders https://www.zoolanders.com; Copyright (C) Joolanders, SL; http://www.gnu.org/licenses/gpl.html GNU/GPL */

import Vue from 'vue';
import api from '@yootheme/api';
import essentials from 'essentials';
import { SourceExtended } from '@essentials/core/source';
import { uuid } from '@essentials/core/util';

/**
 * global queries
 */
Vue.events.on('essentialsDynamicOptions', ({ result = [] }) => {
    const { Schema } = api.builder.helpers;

    for (const globalQuery of essentials?.SourceDynamic?.queries || []) {
        const query = { name: SourceExtended.globalQueryKey(globalQuery.id) };
        const queryName = globalQuery?.source?.query?.name;
        const fieldName = globalQuery?.source?.query?.field?.name;

        const sourceField = Schema.findField(fieldName ? `${queryName}.${fieldName}` : queryName);

        result.push({
            group: Vue.i18n.t('Global'),
            name: `global-query.${globalQuery.id}`,
            title: globalQuery.name,
            meta: sourceField?.metadata.label,
            source: () => sourceField,
            match: ({ prop }) => prop?.query?.name === query.name,
            resolve: ({ name }) => ({ name, query }),
        });
    }

    return result;
});

/**
 * composable source
 */
Vue.events.on(
    'essentialsDynamicOptions',
    ({ result = [] }, { node }) => {
        result.push({
            group: '',
            name: 'composable',
            title: 'Composable Source',
            source: () => false,
            match: ({ prop }) => prop?.composed,
            edit({ prop }) {
                if (node) {
                    Vue.events.trigger('openDynamicPanel', {
                        ...api.customizer.panels['yooessentials-source-composed'],
                        props: {
                            node: node,
                            values: prop.composed,
                        },
                    });
                } else {
                    Vue.events.trigger('openPanel', {
                        ...api.customizer.panels['yooessentials-source-composed'],
                        component: 'fields-panel',
                        props: {
                            values: prop.composed,
                        },
                    });
                }
            },
            onSelection(drop) {
                const prop = {
                    composed: { value: '', sources: {} },
                };

                drop.$emit('resolve', prop);

                this.edit({ prop });
            },
            migrate(prop) {
                const id = uuid(4).toLowerCase();

                prop = {
                    composed: {
                        value: `{{ sources.${id} }}`,
                        sources: {
                            [id]: prop,
                        },
                    },
                };

                return prop;
            },
        });
    },
    -50
);
