import { Component } from '@angular/core';
import { LicenseService } from '@services/license.service';
import { ILicense } from '@red/domain';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { map, tap } from 'rxjs/operators';
import { LoadingService } from '@iqser/common-ui';

const translations = {
    active: _('license-info-screen.status.active'),
    inactive: _('license-info-screen.status.inactive'),
} as const;

@Component({
    selector: 'redaction-license-select',
    templateUrl: './license-select.component.html',
    styleUrls: ['./license-select.component.scss'],
})
export class LicenseSelectComponent {
    value: ILicense;
    licenses$ = this.licenseService.licenseData$.pipe(
        map(data => data.licenses),
        tap(() => {
            if (!this.value) {
                this.value = this.licenseService.activeLicense;
            }
        }),
    );

    constructor(
        readonly licenseService: LicenseService,
        private readonly _loadingService: LoadingService,
    ) {}

    getStatus(id: string): string {
        return id === this.licenseService.activeLicense.id ? translations.active : translations.inactive;
    }

    async licenseChanged(license: ILicense) {
        this._loadingService.start();
        await this.licenseService.loadLicenseData(license);
        this.licenseService.setSelectedLicense(license);
        this._loadingService.stop();
    }
}
