import { Controller } from '@hotwired/stimulus';

/*
* The following line makes this controller "lazy": it won't be downloaded until needed
* See https://symfony.com/bundles/StimulusBundle/current/index.html#lazy-stimulus-controllers
*/

/* stimulusFetch: 'lazy' */
export default class extends Controller {
    static targets = ['button', 'options']
    static outlets = ['result-filter', 'dialog']
    static values = {
        tmdbId: String,
        imdbId: String,
        title: String,
        season: Number,
    }

    toggle() {
        this.optionsTarget.classList.toggle('hidden');
    }

    async monitorSeries() {
        await this.makeMonitor({
            tmdbId: this.tmdbIdValue,
            imdbId: this.imdbIdValue,
            title: this.titleValue,
            monitorType: 'tvshows',
            season: this.seasonValue
        });
        if (this.hasDialogOutlet) {
            this.dialogOutlet.close();
        }
    }

    async makeMonitor(body) {
        const response = await fetch('/api/monitor', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
            },
            body: JSON.stringify(body)
        });
        return await response.json();
    }
}
