import type { SavedData } from "@/types/model";
import { KintoneIndexEventHandler } from "./KintoneIndexEventHandler";

(function (PLUGIN_ID) {
  kintone.events.on('app.record.index.show', (event) => {
    const setting = kintone.plugin.app.getConfig(PLUGIN_ID);
    const config:SavedData = getConfig(setting);
    const handler = new KintoneIndexEventHandler(config);
    handler.init();   
    return event;
  });
  /**
   * Config設定値を変換する
   * @param setting 
   * @returns 
   */
  function getConfig(setting:any):SavedData{
      const config:SavedData={
        buttonName:setting.buttonName,
        joinTables:JSON.parse(setting.joinTables)
      }
      return config;
  }

  function getTestSetting(config:SavedData):SavedData{
      const mainTable =  {
        id:12345,
        app:"140",
        onConditions:[
          {
            leftField:"文字列__1行_",
            rightField:"文字列__1行_"
          }
        ],
        fieldsMapping:[{
          leftField:"",
          rightField:""
        },
        {
          leftField:"",
          rightField:""
        }
        ],
        table:"",
        whereConditions:[
          {
            field:"",
            condition:"",
            data:""
          }
        ]
      };
      const leftJoinTable1 =  {
        id:12345,
        app:"140",
        onConditions:[
          {
            leftField:"文字列__1行_",
            rightField:"文字列__1行_"
          }
        ],
        fieldsMapping:[{
          leftField:"",
          rightField:""
        },
        {
          leftField:"",
          rightField:""
        }
        ],
        table:"",
        whereConditions:[
          {
            field:"",
            condition:"",
            data:""
          }
        ]
      };
      const leftJoinTable2 =  {
        id:12345,
        app:"140",
        onConditions:[
          {
            leftField:"文字列__1行_",
            rightField:"文字列__1行_"
          }
        ],
        fieldsMapping:[{
          leftField:"",
          rightField:""
        },
        {
          leftField:"",
          rightField:""
        }
        ],
        table:"",
        whereConditions:[
          {
            field:"",
            condition:"",
            data:""
          }
        ]
      };
      const retConfig :SavedData ={
          buttonName : config.buttonName,
          joinTables:[
            mainTable,
            leftJoinTable1,
            leftJoinTable2
          ]
      };
      return retConfig;
  }
  
})(kintone.$PLUGIN_ID);

