/* 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 essentials from 'essentials';
import { uuid } from '@essentials/core/util';

export default {
    name: 'StorageModel',

    data: () => ({
        adapters: essentials.customizer?.storage_adapters || {},
    }),

    computed: {
        storages: {
            get() {
                return essentials.Config.get('storages', []);
            },

            set(values) {
                essentials.Config.set('storages', values);
            },
        },
    },

    methods: {
        save(values) {
            const storages = [...this.storages];
            const adapter = this.adapters[values.adapter];

            if (!adapter) {
                throw Error(`Unknown Adapter: ${values.adapter}`);
            }

            if (values.id) {
                storages[storages.findIndex(({ id }) => id === values.id)] = values;
            } else {
                values = {
                    ...values,
                    id: uuid(),
                    _meta: {
                        created_at: Date.now(),
                        created_on: essentials.version,
                    },
                };

                storages.push(values);
            }

            this.storages = storages;

            return values;
        },

        remove(storage) {
            const storages = [...this.storages];
            storages.splice(storages.indexOf(storage), 1);

            this.storages = storages;
        },
    },
};
