/* 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 { isArray } from 'uikit-util';
import { deepCopy, reindex, uuid } from '../../util';
import { ResourceList } from '../../components';
import { ResourceListItem } from '../../components';

export default {
    components: {
        ResourceList,
        ResourceListItem,
    },

    idPrefix: '',

    created() {
        if (!isArray(this.value)) {
            this.value = [];
        }

        this.value.forEach((val, i) => {
            if (!val.id) {
                // workaround for resources created before the id concept
                this.$set(this.value, i, { id: this.getId(), ...val });
            }
        });
    },

    methods: {
        getId() {
            return `${this.$options.idPrefix}${uuid()}`;
        },

        push(value) {
            if (this.$options.idPrefix) {
                value.id = this.getId();
            }

            this.value = [...this.value, value];
        },

        remove(item) {
            this.value.splice(this.value.indexOf(item), 1);
        },

        copy(item) {
            const newItem = deepCopy(item);

            if (this.$options.idPrefix) {
                newItem.id = this.getId();
            }

            if (newItem.props?.name) {
                newItem.props.name = `${newItem.props.name} Copy`
            }

            this.value.splice(this.value.indexOf(item) + 1, 0, newItem);
        },

        move(child, parent, toIndex) {
            const fromIndex = parent.$children.indexOf(child);

            reindex(this.value, fromIndex, toIndex);
        },
    },
};
