import Vue from 'vue'
import api_client_methods from "../../shared/api _client_methods";

export default new Vue({
    data: function () {
        return {
            endpoint: '/admin/fr/builder/api/brick',
            client: {},
        };
    },
    methods: {
        ...api_client_methods.methods,
        // Actions
        addBrick(parentBrickId, brickType, layout) {
            var data = {
                parentBrickId: parentBrickId,
                brickType: brickType,
                layout: layout,
            };
            var url = this.replaceUrlParameters('/create', data)

            return this.post(url, data);
        },
        deleteBrick(brickId) {
            var url = this.replaceUrlParameters('/{brickId}/delete', {brickId: brickId});
            return this.delete(url);
        },
        updateBrickForm(brickId) {
            var url = this.replaceUrlParameters('/{brickId}/update', {brickId: brickId});
            return this.get(url);
        },
        updateBrick(brickId, data) {
            var url = this.replaceUrlParameters('/{brickId}/update', {brickId: brickId});
            return this.post(url, data);
        },
        duplicateBrick(brickId, options= []) {
            var url = this.replaceUrlParameters('/{brickId}/duplicate', {brickId: brickId});

            if (options.length > 0) {
                var params = [];
                options.forEach((option) => {
                    params.push(option.key + '=' + option.value);
                });
                url += '?' + params.join('&');
            }

            return this.get(url);
        },
        dragBrick(brickId, parentBrickId, eventType, index) {
            var url = this.replaceUrlParameters('/{brickId}/sort', {brickId: brickId});
            var data = {
                brickId: brickId,
                parentBrickId: parentBrickId,
                eventType: eventType,
                index: index,
            };
            return this.post(url, data);
        },
        templateBrick(brickId, data) {
            var url = this.replaceUrlParameters('/{brickId}/template', {brickId: brickId});
            return this.post(url, data);
        },
        getLayouts() {
            return this.get('/layout');
        },
        updateLayoutBrick(brickId, data) {
            var url = this.replaceUrlParameters('/{brickId}/layout', {brickId: brickId});
            return this.post(url, data);
        }
    },
})
