import type { ButtonComponent, Component } from "../types/component";
import type { Project } from "../types/project";
import type { EquationSides, ActionElementType } from "./rdlTypes";
// コンポーネントがButtonかどうかの判別メゾット(filter用)
const isButton = (c: Component): c is ButtonComponent => c.type === 'button'
// React用のコンポーネントデータをRDL出力用に変換するメゾット
const toRecord = (b: ButtonComponent): ActionElementType | undefined => {
const curBase = { curWId: b.name, curScreenId: b.screenId};
switch (b.action?.type) {
case "navigate":
return {...curBase, actionType: "navigate", nextScreenId: b.action.action.nextScreenName};
case "addData":
return {...curBase, actionType: "addData", addData: b.action.action.newData, addTableId: b.action.action.tableName, successScreenId: b.action.action.successScreenName, failedScreenId: b.action.action.failScreenName};
case "updateData":
return {...curBase, actionType: "updateData", updateData: b.action.action.newData, updateTableId: b.action.action.tableName, successScreenId: b.action.action.successScreenName, failedScreenId: b.action.action.failScreenName};
case "deleteData":
return {...curBase, actionType: "deleteData", deleteData: b.action.action.deleteData, deleteTableId: b.action.action.tableName, successScreenId: b.action.action.successScreenName, failedScreenId: b.action.action.failScreenName}
case "searchData":
return {...curBase, actionType: "searchData", searchTableId: b.action.action.tableName, searchData: b.action.action.searchData, targetWId: b.action.action.targetComponentName
}
default:
return undefined;
}
}
// Reactコンポーネントをfilterにかけ、RDL出力用に変換
export const collectRecords = (components:Component[]): ActionElementType[] =>
components.filter(isButton).map(toRecord).filter((r): r is ActionElementType => r !== undefined);
// RDL出力用に変換されたオブジェクトをRDL式(左辺, 右辺)に変換するメソッド
const formatRecordToTerm = (r: ActionElementType): EquationSides => {
switch (r.actionType) {
case "navigate":
return {leftSide: `"${r.nextScreenId}"`, rightSide: `[curScId' : [[wState : wId' -> "${r.curWId}"] : scId' -> "${r.curScreenId}"] -> "clicked"]`}
case "addData":
return {leftSide: "", rightSide: ""}
case "updateData":
return {leftSide: "", rightSide: ""}
case "deleteData":
return {leftSide: "", rightSide: ""}
case "searchData":
return {leftSide: "", rightSide: ""}
default: {
const _exhaustive: never = r;
return _exhaustive;
}
}
}
export const buildRDLText = (records: ActionElementType[]): string => records.map(formatRecordToTerm)
.filter(({ leftSide, rightSide }) => leftSide !== "" || rightSide !== "")
.map(({ leftSide, rightSide }) => `${leftSide} = ${rightSide}`)
.join("\n");
export const toOutputRDL = (p: Project): string => {
const records = p.screens.map((s) => collectRecords(s.components)).flat();
return buildRDLText(records)
}