import { ChangeDetectionStrategy, Component, EventEmitter, Input, Output } from '@angular/core';
import { UserService } from '@users/user.service';
import { User } from '@red/domain';
import { List } from '@iqser/common-ui/lib/utils';

@Component({
    selector: 'redaction-assign-user-dropdown',
    templateUrl: './assign-user-dropdown.component.html',
    styleUrls: ['./assign-user-dropdown.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class AssignUserDropdownComponent {
    private _currentUser: User | string;
    oldUser: User | string;
    @Input() options: List<User | string>;
    @Output() readonly save = new EventEmitter<User | string>();
    @Output() readonly cancel = new EventEmitter();

    constructor(private readonly _userService: UserService) {}

    get value(): User | string {
        return this._currentUser;
    }

    @Input()
    set value(value: User | string) {
        if (this.oldUser === undefined) {
            this.oldUser = value;
        }
        this._currentUser = value;
    }

    getContext(user: User | string) {
        return { userId: typeof user === 'string' ? user : user?.id };
    }
}
