import { INIT_ACTION_ADVANCED_CONFIG, INIT_ACTION_MOCK_CONFIG } from "@/page/App/components/publicConfig/action"
import { ACTION_RUN_TIME, ActionContent, ActionItem } from "@/redux/currentApp/action/interface"
import { klona } from "klona/json"
import AsyncStorage from '@react-native-async-storage/async-storage';

export const fixedActionToNewAction = (
  actions?: ActionItem<ActionContent>[],
) => {
  return (
    actions?.map((action) => {
      if (action.actionType === "transformer") {
        return action
      }
      const advancedConfig = klona(INIT_ACTION_ADVANCED_CONFIG)
      if (action.triggerMode === "automate") {
        advancedConfig.runtime = ACTION_RUN_TIME.APP_LOADED
      }
      return {
        ...action,
        config: {
          ...action.config,
          public: !!action.config?.public,
          advancedConfig: action.config?.advancedConfig ?? advancedConfig,
          mockConfig: action.config?.mockConfig ?? INIT_ACTION_MOCK_CONFIG,
        },
      }
    }) ?? []
  )
}


// async (store, dynamicData, userValue, body) => {


//   if (store?.ctx?.core?.data?.Actions === 'Submit All' || store?.ctx?.core?.data?.Actions === 'Reject All') {

//     return true;

//   }

//   store?.setNotify({ FailMessage: "Please select all fields", Fail: true });
//   return false
// };

// async (store, dynamicData, userValue, body) => {
//   if (store?.ctx?.core?.data?.Actions === 'Submit' || store?.ctx?.core?.data?.Actions === 'Reject') {
//     const find = store?.ctx?.core?.data?.pending_report.find((e) => e.Select)
//     if (find) {
//       return true
//     } else {
//       store?.setNotify({ FailMessage: "Please Select Row", Fail: true });
//     }

//   }else{
//       store?.setNotify({ FailMessage: "Please select all fields", Fail: true });
//   }

// };   


async (
  store,
  dynamicData,
  userValue,
  parentEventOutput,
  service,
  componentName
) => {
  // Get and parse local storage data
  // const localData = JSON.parse(localStorage.getItem("rowData"));
  // const localData = JSON.parse(await AsyncStorage.getItem("rowData"));
  const data = await AsyncStorage.getItem("rowData");
  const localData = JSON.parse(data);
  // Extract all required fields from localData
  const {
    status,
    invoiceNo,
    invoice_id,
    month,
    source_state,
    source_gstn,
    amount,
    invoice_date,
    destination_state,
    destination_gstn,
    cgst,
    sgst,
    igst,
    total_invoice_value,
    total_gross_amount,
    gst_applicable,
    programId
  } = localData;
  let invoiceDate = invoice_date;
  if (invoiceDate) {
    const dateObj = new Date(invoiceDate);
    const formattedInvoiceDate = dateObj.toISOString();
    invoiceDate = formattedInvoiceDate;
  }
  const disable = !(status === "I" || status === "Initial");

  const dataDemo1 = {
    invoice_id,
    month,
    source_state,
    source_gstn,
    amount,
    destination_state,
    cgst,
    sgst,
    igst,
    total_invoice_value,
    total_gross_amount,
    gst_applicable,
    programId
  }
  if (invoiceNo && destination_gstn && invoiceDate) {
    store.setFormdata((prev) => ({ ...prev, ...dataDemo1, invoice_date: invoiceDate, destination_gstn, invoiceNo }));
  } else if (invoiceDate) {
    store.setFormdata((prev) => ({ ...prev, ...dataDemo1, invoice_date: invoiceDate }));
  }
  else {
    store.setFormdata((prev) => ({ ...prev, ...dataDemo1 }));
  }

  store.setSchema((prev) => ({
    ...prev,
    properties: {
      signedInvoiceUpload: {
        ...prev.properties?.signedInvoiceUpload,
        disabled: disable,
      },
      submit: {
        ...prev.properties?.submit,
        disabled: disable,
      },
      invoiceStatus: {
        ...prev.properties?.invoiceStatus,
        disabled: true,
      },
      Save: {
        ...prev.properties?.Save,
        disabled: disable
      },
      invoiceNo: {
        ...prev.properties?.invoiceNo,
        disabled: disable
      },
      month: {
        ...prev.properties?.month,
        disabled: true
      },
      source_state: {
        ...prev.properties?.source_state,
        disabled: true
      },
      source_gstn: {
        ...prev.properties?.source_gstn,
        disabled: true
      },
      amount: {
        ...prev.properties?.amount,
        disabled: true
      },
      total_gross_amount: {
        ...prev.properties?.total_gross_amount,
        disabled: true
      },
      cgst: {
        ...prev.properties?.cgst,
        disabled: true
      },
      sgst: {
        ...prev.properties?.sgst,
        disabled: true
      },
      igst: {
        ...prev.properties?.igst,
        disabled: true
      },
      destination_state: {
        ...prev.properties?.destination_state,
        disabled: true
      },
      destination_gstn: {
        ...prev.properties?.destination_gstn,
        disabled: disable
      },
      total_invoice_value: {
        ...prev.properties?.total_invoice_value,
        disabled: true
      },
    },
  }));

};