/**
 * AtroCore Software
 *
 * This source file is available under GNU General Public License version 3 (GPLv3).
 * Full copyright and license information is available in LICENSE.txt, located in the root directory.
 *
 * @copyright  Copyright (c) AtroCore GmbH (https://www.atrocore.com)
 * @license    GPLv3 (https://www.gnu.org/licenses/)
 */

Espo.define('views/layout/fields/view-type', 'views/fields/enum', function (Dep) {

    return Dep.extend({
        setup: function () {
            this.params.translation = 'Admin.layouts';
            this.params.groupTranslation = 'Layout.groups.viewType'

            Dep.prototype.setup.call(this);
            if (!this.model.get(this.name) && this.params.options.length) {
                this.model.set(this.name, this.params.options[0])
            }

            this.listenTo(this.model, 'change:entity', () => {
                this.setupGroups()
                this.setupTranslation()
                this.reRender()
            })
        },

        setupGroups() {
            this.params.options = this.getAvailableOptions()
            this.params.groupOptions = [
                {
                    name: "view",
                    options: this.params.options.filter(o => !(["relationships","leftSidebar",  "rightSideView"].includes(o)))
                },
                {
                    name: "viewGroup",
                    options: ["relationships", "leftSidebar", "rightSideView"]
                }
            ]
        },

        getAvailableOptions() {
            const optionList = [
                "list",
                "detail",
                "relationships",
                "leftSidebar",
                "rightSideView"
            ]
            if (this.getMetadata().get(['clientDefs', this.model.get('entity'), 'kanbanViewMode'])) {
                optionList.push("kanban")
            }
            Object.keys(this.getMetadata().get(['clientDefs', this.model.get('entity'), 'additionalLayouts']) || {})
                .forEach(layout => {
                    optionList.push(layout)

                })

            return optionList
        }
    });
});

