import type {Workspace, Descriptor} from '@yarnpkg/core';
import {structUtils} from '@yarnpkg/core';

export type Operation = 'add' | 'remove' | 'replace';


export type OperationReporterOptions = {
    operation: OperationReporter['operation'],
    workspace: OperationReporter['workspace'],
    target: OperationReporter['target'],
    descriptor: OperationReporter['descriptor'],
    destinationDescriptor: OperationReporter['destinationDescriptor']
}

export abstract class OperationReporter {
    protected operation: Operation;
    protected workspace: Workspace;
    protected target: string;
    protected descriptor: Descriptor;
    protected destinationDescriptor?: Descriptor;
    protected formattedFields: Record<'workspace' | 'dependency', string>
    protected formattedWorkspace: string;
    protected formattedDependency: string;

    constructor({operation, workspace, target, descriptor, destinationDescriptor}: OperationReporterOptions) {
        this.operation = operation;
        this.workspace = workspace;
        this.target = target;
        this.descriptor = descriptor;
        this.destinationDescriptor = destinationDescriptor;

        this.initFormattedFields();
    }

    protected initFormattedFields = () => {
        this.formattedFields = {
            workspace: structUtils.prettyWorkspace(this.workspace.project.configuration, this.workspace),
            dependency: structUtils.prettyDescriptor(this.workspace.project.configuration, this.descriptor),
        }

        if (this.operation === 'replace') {
            this.formattedFields.dependency = `${this.formattedFields.dependency} with ${structUtils.prettyDescriptor(this.workspace.project.configuration, this.destinationDescriptor)}`
        }
    }

    abstract reportOperation(): void;
}