* Copyright (c) 2024-2025 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.
*/
import * as idl from "@idlizer/core"
import { Language, LayoutNodeRole, PeerClass, PeerLibrary } from "@idlizer/core";
import { collapseSameNamedMethods, collectComponents, componentToPeerClass, ImportsCollector, PrinterResult, readLangTemplate, OverloadsPrinter, peerGeneratorConfiguration, groupOverloads, collectPeersForFile } from "@idlizer/libohos";
import { ArkoalaPeerLibrary } from "../ArkoalaPeerLibrary";
import { generateArkComponentName, shiftIfIsNotEmpty } from "./ComponentsPrinter";
function printETSComponent(library: PeerLibrary, peer: PeerClass, isDeclaration: boolean): PrinterResult{
const component = collectComponents(library).find(it => it.name === peer.componentName)!
const generate = () => {
const writer = library.createLanguageWriter(Language.ARKTS)
const callableMethods = peer.methods.filter(it => it.isCallSignature).map(it => it.method)
const callableMethod = callableMethods.length ? collapseSameNamedMethods(callableMethods) : undefined
const mappedCallableParams = callableMethod?.signature.args.map((it, index) => `${callableMethod.signature.argName(index)}${idl.isOptionalType(it) ? "?" : ""}: ${writer.getNodeName(it)}`)
const mappedCallableParamsValues = callableMethod?.signature.args.map((_, index) => callableMethod.signature.argName(index))
const componentClassName = generateArkComponentName(peer.componentName)
const peerClassName = componentToPeerClass(peer.componentName)
writer.writeLines(readLangTemplate(isDeclaration ? "ets_component_decl.d.ets" : "ets_component_impl.ets", Language.ARKTS)
.replaceAll("%COMPONENT_NAME%", component.name)
.replaceAll("%FUNCTION_PARAMETERS%", shiftIfIsNotEmpty(mappedCallableParams?.map(it => `${it}, `).join("") ?? ""))
.replaceAll("%COMPONENT_CLASS_NAME%", componentClassName)
.replaceAll("%PEER_CLASS_NAME%", peerClassName)
.replaceAll("%PEER_CALLABLE_INVOKE%", callableMethod?.name ? `receiver.${callableMethod?.name}(${mappedCallableParamsValues})` : ""))
return writer
}
return {
over: {
node: component.attributeDeclaration,
role: LayoutNodeRole.INTERFACE,
},
generate,
weight: 99
}
}
export function printETSDeclaration(library: PeerLibrary): PrinterResult[] {
return library.files.flatMap<PrinterResult>(file =>
collectPeersForFile(library, file).filter(it => it.originalInterfaceName).flatMap<PrinterResult>(peer =>
printETSComponent(library, peer, true)))
}