/* 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 isEmpty from 'lodash-es/isEmpty';

export default function (FieldSourceQueryArgs) {
    return {
        name: 'YooessentialsSourceQueryArgs',

        functional: true,

        render: function (createElement, { parent: { node } }) {
            const { Schema, Source } = api.builder.helpers;
            const query = Source.getQuery(node);
            const fields = Schema.findField(query)?.metadata?.fields;

            if (isEmpty(fields)) {
                return null;
            }

            // init arguments namespace
            if (node?.source?.query && !node?.source?.query?.arguments) {
                Vue.set(node.source.query, 'arguments', {});
            }

            return FieldSourceQueryArgs.render(createElement, { parent: { node } });
        },
    };
}
