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

/*
* The following line makes this controller "lazy": it won't be downloaded until needed
* See https://github.com/symfony/stimulus-bridge#lazy-controllers
*/
/* stimulusFetch: 'lazy' */
export default class extends Controller {
    H264_CODECS = ['h264', 'h.264', 'x264']
    H265_CODECS = ['h265', 'h.265', 'x265', 'hevc']

    static values = {
        title: String,
        tmdbId: String,
        imdbId: String,
        season: String,
        episode: String,
        active: Boolean,
    };

    static targets = ['list', 'count', 'episodeSelector',]

    options = []

    listTargetConnected() {
        this.element.options = this.element.querySelectorAll('tbody tr');
        if (this.element.options.length > 0) {
            this.element.options.forEach((option) =>
                option.querySelector('.download-btn').dataset['title'] = this.titleValue
            );
            this.element.options[0].querySelector('input[type="checkbox"]').checked = true;
            document.dispatchEvent(new CustomEvent('optionsLoaded', {detail: {options: this.element.options}}));
        } else {
            this.countTarget.innerText = 0;
            this.episodeSelectorTarget.disabled = true;
        }
    }
}
