/**
 * Copyright (c) 2020-present, Goldman Sachs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { observable, action, computed, makeObservable, flow } from 'mobx';
import { uuid } from '@finos/legend-shared';
import { SourceInformation, CodeCompletionResult, } from '@finos/legend-graph';
/**
 * This is not strictly meant for lambda. The idea is to create an editor that allows
 * editing _something_ but allows user to edit via text.
 */
export class LambdaEditorState {
    uuid = uuid();
    lambdaPrefix;
    lambdaString; // value shown in lambda editor which can be edited
    parserError;
    compilationError;
    typeAheadEnabled = false;
    constructor(lambdaString, lambdaPrefix, options) {
        makeObservable(this, {
            lambdaString: observable,
            parserError: observable,
            compilationError: observable,
            typeAheadEnabled: observable,
            lambdaId: computed,
            fullLambdaString: computed,
            setLambdaString: action,
            setTypeAhead: action,
            clearErrors: action,
            setCompilationError: action,
            setParserError: action,
            convertLambdaGrammarStringToObject: flow,
            convertLambdaObjectToGrammarString: flow,
        });
        this.lambdaString = lambdaString;
        this.lambdaPrefix = lambdaPrefix;
        this.typeAheadEnabled = options?.typeAheadEnabled ?? false;
    }
    // value shown in lambda editor with the prefix, used to send to the server to transformation from text to JSON
    get fullLambdaString() {
        return `${this.lambdaPrefix}${this.lambdaString}`;
    }
    setLambdaString(val) {
        this.lambdaString = val;
    }
    setTypeAhead(val) {
        this.typeAheadEnabled = val;
    }
    clearErrors(options) {
        this.setParserError(undefined);
        if (options?.preserveCompilationError && this.compilationError) {
            this.compilationError.sourceInformation = undefined;
        }
        else {
            this.setCompilationError(undefined);
        }
    }
    setCompilationError(compilationError) {
        // account for the lambda prefix offset in source information
        if (compilationError?.sourceInformation) {
            compilationError.sourceInformation = this.processSourceInformation(compilationError.sourceInformation);
        }
        this.compilationError = compilationError;
    }
    setParserError(parserError) {
        // account for the lambda prefix offset in source information
        if (parserError?.sourceInformation) {
            parserError.sourceInformation = this.processSourceInformation(parserError.sourceInformation);
        }
        this.parserError = parserError;
    }
    processSourceInformation(sourceInformation) {
        const { sourceId, startLine, startColumn, endLine, endColumn } = sourceInformation;
        const lineOffset = 0;
        const columnOffset = this.lambdaPrefix.length;
        return new SourceInformation(sourceId, startLine + lineOffset, startColumn - (startLine === 1 ? columnOffset : 0), endLine + lineOffset, endColumn - (endLine === 1 ? columnOffset : 0));
    }
    extractLambdaString(fullLambdaString) {
        return fullLambdaString.substring(fullLambdaString.indexOf(this.lambdaPrefix) + this.lambdaPrefix.length, fullLambdaString.length);
    }
    async getCodeComplete(input) {
        return Promise.resolve(new CodeCompletionResult());
    }
}
//# sourceMappingURL=LambdaEditorState.js.map