1/*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import ts from 'typescript';
17import path from 'path';
18
19import { projectConfig } from '../main';
20import { resourceFileName } from './process_ui_syntax';
21import {
22  PAGE_PATH,
23  INTEGRATED_HSP,
24  TRUE,
25  FALSE,
26  RESOURCE_NAME_BUNDLE,
27  RESOURCE_NAME_MODULE
28} from './pre_define';
29import constantDefine from './constant_define';
30
31export function routerOrNavPathWrite(context: ts.TransformationContext, keyName: string, projectPath: string,
32  projectRootPath: string = ''): ts.PropertyAssignment {
33  return context.factory.createPropertyAssignment(
34    context.factory.createIdentifier(keyName),
35    context.factory.createStringLiteral(
36      projectConfig.compileHar ? keyName === PAGE_PATH ? byteCodeHarPagePath(projectRootPath) : '' :
37        pathMessage(projectPath))
38  );
39}
40
41function byteCodeHarPagePath(projectRootPath: string): string {
42  return projectConfig.byteCodeHar ? pathMessage(projectRootPath) : constantDefine.HAR_DEFAULT_PAGE_PATH;
43}
44
45function pathMessage(projectPathName: string): string {
46  return path.relative(projectPathName || '', resourceFileName).replace(/\\/g, '/').replace(/\.ets$/, '');
47}
48
49export function integratedHspType(): string {
50  return projectConfig.integratedHsp ? TRUE : projectConfig.compileHar ? constantDefine.HAR_DEFAULT_INTEGRATED_HSP_TYPE : FALSE;
51}
52
53export function routerModuleType(context: ts.TransformationContext): ts.PropertyAssignment {
54  return context.factory.createPropertyAssignment(
55    context.factory.createIdentifier(constantDefine.MODULE_TYPE),
56    context.factory.createStringLiteral(moduleType())
57  );
58}
59
60function moduleType(): string {
61  if (projectConfig.compileHar) {
62    return projectConfig.byteCodeHar ? constantDefine.BYTE_CODE_HAR : constantDefine.CLOSED_SOURCE_HAR;
63  } else if (projectConfig.compileShared) {
64    return projectConfig.integratedHsp ? INTEGRATED_HSP : constantDefine.SHARED_HSP;
65  }
66  return constantDefine.FOLLOW_WITH_HAP;
67}
68
69export function routerBundleOrModule(context: ts.TransformationContext, isByteCodeHar: boolean, type: string): ts.PropertyAssignment {
70  const typeKey: string = type === RESOURCE_NAME_BUNDLE ? RESOURCE_NAME_BUNDLE : RESOURCE_NAME_MODULE;
71  if (isByteCodeHar) {
72    return context.factory.createPropertyAssignment(
73      context.factory.createIdentifier(typeKey),
74      context.factory.createIdentifier(type === RESOURCE_NAME_BUNDLE ? '__BUNDLE_NAME__' : '__MODULE_NAME__')
75    );
76  }
77  return context.factory.createPropertyAssignment(
78    context.factory.createIdentifier(typeKey),
79    context.factory.createStringLiteral(type === RESOURCE_NAME_BUNDLE ? (projectConfig.bundleName || '') : (projectConfig.moduleName || ''))
80  );
81}
82