/**
 * Copyright (c) 2025-2026 Huawei Device Co., Ltd.
 * 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.
 */

#include <iostream>
#include <ostream>
#include <string>

#include "libarkbase/os/library_loader.h"

#include "public/es2panda_lib.h"
#include "./util.h"

// NOLINTBEGIN

static es2panda_Impl *impl = nullptr;

int RunDeclgen(es2panda_TsDeclgen *declgen, es2panda_Context *context)
{
    if (auto result = impl->GenerateTsDeclarationsAfterParsed(declgen); result != 0) {
        std::cerr << "FAILED TO GENERATE DECLARATIONS AFTER PARSED" << std::endl;
        return result;
    }
    impl->ProceedToState(context, ES2PANDA_STATE_CHECKED);
    CheckForErrors("CHECKED", context);
    if (auto result = impl->GenerateTsDeclarationsAfterCheck(declgen); result != 0) {
        std::cerr << "FAILED TO GENERATE DECLARATIONS AFTER CHECKED" << std::endl;
        return result;
    }
    if (auto result = impl->WriteTsDeclarations(declgen); result != 0) {
        std::cerr << "FAILED TO WRITE GENERATED DECLARATIONS" << std::endl;
        return result;
    }
    return 0;
}

int main(int argc, char **argv)
{
    es2panda_TsDeclgen *declgen = nullptr;
    if (argc < MIN_ARGC) {
        return INVALID_ARGC_ERROR_CODE;
    }

    if (GetImpl() == nullptr) {
        return NULLPTR_IMPL_ERROR_CODE;
    }
    impl = GetImpl();
    std::cout << "LOAD SUCCESS" << std::endl;

    const char **args = const_cast<const char **>(&(argv[1]));
    auto config = impl->CreateConfig(argc - 1, args);
    auto context = impl->CreateContextFromFile(config, argv[argc - 1]);
    if (context == nullptr) {
        std::cerr << "FAILED TO CREATE CONTEXT" << std::endl;
        impl->DestroyConfig(config);
        return NULLPTR_CONTEXT_ERROR_CODE;
    }

    impl->ProceedToState(context, ES2PANDA_STATE_PARSED);
    CheckForErrors("PARSED", context);

    std::string declName = GetDeclPrefix(argv[argc - 1]) + ".d.ets";
    const char *inputFiles[] = {argv[argc - 1]};
    const char *outputDeclEts[] = {declName.c_str()};
    const char *outputEts[] = {"dump.ets"};
    declgen = impl->CreateTsDeclgen(context, 1, inputFiles, outputDeclEts, outputEts, false, true, "", true);
    if (declgen == nullptr) {
        std::cerr << "[ERROR] FAILED TO CREATE DECLGEN for output: " << declName << std::endl;
    }
    std::cout << "[INFO] Created declgen for output file: " << declName << std::endl;

    auto result = RunDeclgen(declgen, context);

    impl->DestroyTsDeclgen(declgen);
    impl->DestroyContext(context);
    impl->DestroyConfig(config);
    return result;
}

// NOLINTEND