/* 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 api from '@yootheme/api';
import essentials from 'essentials';
import { FieldsPanel } from '@yootheme/sidebar';

export default {
    extends: FieldsPanel,

    created() {
        this.builder = getBuilder();
    },

    provide() {
        return {
            $node: this.node,
            Builder: getBuilder(),
        };
    },

    props: {
        node: Object,
        Source: {
            type: Object,
            default: () => api.builder.helpers.Source,
        },
    },

    methods: {
        prepare(config) {
            return FieldsPanel.methods.prepare.call(this, config).map((field) => {
                this.$trigger('yooessentialsBuilderPrepareField', { node: this.node, field });

                return field;
            });
        },
    },
};

// map Builder or mock it for when is required outside a Builder Section
function getBuilder() {
    return (
        essentials.Builder || {
            isPage: () => false,
            path: (n) => [n],
            type: () => ({}),
        }
    );
}
