text: import { httpRequest } from "./api";
  import {
    convertDateToEpoch,
    getCurrentFinancialYear,
    getCheckBoxJsonpath,
    getSafetyNormsJson,
    getHygeneLevelJson,
    getLocalityHarmedJson,
    setFilteredTradeTypes,
    getTradeTypeDropdownData
  } from "../ui-config/screens/specs/utils";
  import {
    prepareFinalObject,
    toggleSnackbar
  } from "egov-ui-framework/ui-redux/screen-configuration/actions";
  import {
    getTranslatedLabel,
    //updateDropDowns,
    ifUserRoleExists
  } from "../ui-config/screens/specs/utils";
  import { handleScreenConfigurationFieldChange as handleField } from "egov-ui-framework/ui-redux/screen-configuration/actions";
  import store from "../ui-redux/store";
  import get from "lodash/get";
  import set from "lodash/set";
  import {
    getQueryArg,
    getFileUrlFromAPI
  } from "egov-ui-framework/ui-utils/commons";
  import { getTenantId } from "egov-ui-kit/utils/localStorageUtils";
  import { setBusinessServiceDataToLocalStorage, getFileUrl, enableFieldAndHideSpinner } from "egov-ui-framework/ui-utils/commons";
  import { getPaymentSearchAPI } from "egov-ui-kit/utils/commons";
  
  export const updateTradeDetails = async requestBody => {
    try {
      const payload = await httpRequest(
        "post",
        "/tl-services/v1/_update",
        "",
        [],
        requestBody
      );
      return payload;
    } catch (error) {
      store.dispatch(toggleSnackbar(true, error.message, "error"));
    }
  };
  
  export const getLocaleLabelsforTL = (label, labelKey, localizationLabels) => {
    if (labelKey) {
      let translatedLabel = getTranslatedLabel(labelKey, localizationLabels);
      if (!translatedLabel || labelKey === translatedLabel) {
        return label;
      } else {
        return translatedLabel;
      }
    } else {
      return label;
    }
  };
  
  export const getSearchResults = async queryObject => {
    try {
      let businessService = '';
      queryObject && Array.isArray(queryObject) && queryObject.map(query => {
        if (query.key == "businessServices") {
          businessService = query.value;
          if (typeof businessService == 'object') {
            query.value = businessService.join();
          }
        }
      })
      if (typeof businessService == 'string') {
        const response = await httpRequest(
          "post",
          getPaymentSearchAPI(businessService),
          "",
          queryObject
        );
  
        return response;
      } else if (process.env.REACT_APP_NAME === "Citizen"){
        const response = await httpRequest(
          "post",
          getPaymentSearchAPI('-1'),
          "",
          queryObject
        );
  
        return response;
      }else if (typeof businessService == 'object') {
        const response = { "Payments": [] };
        businessService.map(async (businessSer) => {
          try {
            let respo = await httpRequest(
              "post",
              getPaymentSearchAPI(businessSer),
              "",
              queryObject
            )
            response.Payments.push(...respo.Payments);
  
          } catch (e) {
            console.log(e);
          }
        })
        if (response.Payments.length == 0) {
          throw { message: 'PAYMENT_SEARCH_FAILED' };
        }
        return response;
      }
  
    } catch (error) {
      enableFieldAndHideSpinner('search', "components.div.children.UCSearchCard.children.cardContent.children.buttonContainer.children.searchButton", store.dispatch);
      console.error(error);
      store.dispatch(
        toggleSnackbar(
          true,
          { labelName: error.message, labelCode: error.message },
          "error"
        )
      );
    }
  };
  
  const setDocsForEditFlow = async (state, dispatch) => {
    const applicationDocuments = get(
      state.screenConfiguration.preparedFinalObject,
      "Licenses[0].tradeLicenseDetail.applicationDocuments",
      []
    );
    let uploadedDocuments = {};
    let fileStoreIds =
      applicationDocuments &&
      applicationDocuments.map(item => item.fileStoreId).join(",");
    const fileUrlPayload =
      fileStoreIds && (await getFileUrlFromAPI(fileStoreIds));
    applicationDocuments &&
      applicationDocuments.forEach((item, index) => {
        uploadedDocuments[index] = [
          {
            fileName:
              (fileUrlPayload &&
                fileUrlPayload[item.fileStoreId] &&
                decodeURIComponent(
                  getFileUrl(fileUrlPayload[item.fileStoreId])
                    .split("?")[0]
                    .split("/")
                    .pop()
                    .slice(13)
                )) ||
              `Document - ${index + 1}`,
            fileStoreId: item.fileStoreId,
            fileUrl: Object.values(fileUrlPayload)[index],
            documentType: item.documentType,
            tenantId: item.tenantId,
            id: item.id
          }
        ];
      });
    dispatch(
      prepareFinalObject("LicensesTemp[0].uploadedDocsInRedux", uploadedDocuments)
    );
  };
  
  export const getBoundaryData = async (
    action,
    state,
    dispatch,
    queryObject,
    code,
    componentPath
  ) => {
    try {
      let payload = await httpRequest(
        "post",
        "/egov-location/location/v11/boundarys/_search?hierarchyTypeCode=REVENUE&boundaryType=Locality",
        "_search",
        queryObject,
        {}
      );
      const tenantId =
        process.env.REACT_APP_NAME === "Employee"
          ? get(
            state.screenConfiguration.preparedFinalObject,
            "Licenses[0].tradeLicenseDetail.address.city"
          )
          : getQueryArg(window.location.href, "tenantId");
  
      const mohallaData =
        payload &&
        payload.TenantBoundary[0] &&
        payload.TenantBoundary[0].boundary &&
        payload.TenantBoundary[0].boundary.reduce((result, item) => {
          result.push({
            ...item,
            name: `${tenantId
              .toUpperCase()
              .replace(/[.]/g, "_")}_REVENUE_${item.code
                .toUpperCase()
                .replace(/[._:-\s\/]/g, "_")}`
          });
          return result;
        }, []);
  
      dispatch(
        prepareFinalObject(
          "applyScreenMdmsData.tenant.localities",
          // payload.TenantBoundary && payload.TenantBoundary[0].boundary,
          mohallaData
        )
      );
  
      dispatch(
        handleField(
          "apply",
          "components.div.children.formwizardFirstStep.children.tradeLocationDetails.children.cardContent.children.tradeDetailsConatiner.children.tradeLocMohalla",
          "props.suggestions",
          mohallaData
        )
      );
      if (code) {
        let data = payload.TenantBoundary[0].boundary;
        let messageObject =
          data &&
          data.find(item => {
            return item.code == code;
          });
        if (messageObject)
          dispatch(
            prepareFinalObject(
              "Licenses[0].tradeLicenseDetail.address.locality.name",
              messageObject.name
            )
          );
      }
    } catch (e) {
      console.log(e);
    }
  };
  
  const createOwnersBackup = (dispatch, payload) => {
    const owners = get(payload, "Licenses[0].tradeLicenseDetail.owners");
    owners &&
      owners.length > 0 &&
      dispatch(
        prepareFinalObject(
          "LicensesTemp[0].tradeLicenseDetail.owners",
          JSON.parse(JSON.stringify(owners))
        )
      );
  };
  
  const getMultiUnits = multiUnits => {
    let hasTradeType = false;
    let hasAccessoryType = false;
  
    let mergedUnits =
      multiUnits &&
      multiUnits.reduce((result, item) => {
        hasTradeType = item.hasOwnProperty("tradeType");
        hasAccessoryType = item.hasOwnProperty("accessoryCategory");
        if (item && item !== null && (hasTradeType || hasAccessoryType)) {
          if (item.hasOwnProperty("id")) {
            if (item.hasOwnProperty("active") && item.active) {
              if (item.hasOwnProperty("isDeleted") && !item.isDeleted) {
                set(item, "active", false);
                result.push(item);
              } else {
                result.push(item);
              }
            }
          } else {
            if (!item.hasOwnProperty("isDeleted")) {
              result.push(item);
            }
          }
        }
        return result;
      }, []);
  
    return mergedUnits;
  };
  
  // const getMultipleAccessories = licenses => {
  //   let accessories = get(licenses, "tradeLicenseDetail.accessories");
  //   let mergedAccessories =
  //     accessories &&
  //     accessories.reduce((result, item) => {
  //       if (item && item !== null && item.hasOwnProperty("accessoryCategory")) {
  //         if (item.hasOwnProperty("id")) {
  //           if (item.hasOwnProperty("active") && item.active) {
  //             if (item.hasOwnProperty("isDeleted") && !item.isDeleted) {
  //               set(item, "active", false);
  //               result.push(item);
  //             } else {
  //               result.push(item);
  //             }
  //           }
  //         } else {
  //           if (!item.hasOwnProperty("isDeleted")) {
  //             result.push(item);
  //           }
  //         }
  //       }
  //       return result;
  //     }, []);
  
  //   return mergedAccessories;
  // };
  
  const getMultipleOwners = owners => {
    let mergedOwners =
      owners &&
      owners.reduce((result, item) => {
        if (item && item !== null && item.hasOwnProperty("mobileNumber")) {
          if (item.hasOwnProperty("active") && item.active) {
            if (item.hasOwnProperty("isDeleted") && !item.isDeleted) {
              set(item, "active", false);
              result.push(item);
            } else {
              result.push(item);
            }
          } else {
            if (!item.hasOwnProperty("isDeleted")) {
              result.push(item);
            }
          }
        }
        return result;
      }, []);
  
    return mergedOwners;
  };
  
  export const applyTradeLicense = async (state, dispatch, activeIndex) => {
    try {
      let queryObject = JSON.parse(
        JSON.stringify(
          get(state.screenConfiguration.preparedFinalObject, "Licenses", [])
        )
      );
      let documents = get(
        queryObject[0],
        "tradeLicenseDetail.applicationDocuments"
      );
      set(
        queryObject[0],
        "validFrom",
        convertDateToEpoch(queryObject[0].validFrom, "dayend")
      );
      set(queryObject[0], "wfDocuments", documents);
      set(
        queryObject[0],
        "validTo",
        convertDateToEpoch(queryObject[0].validTo, "dayend")
      );
      if (queryObject[0] && queryObject[0].commencementDate) {
        queryObject[0].commencementDate = convertDateToEpoch(
          queryObject[0].commencementDate,
          "dayend"
        );
      }
      let owners = get(queryObject[0], "tradeLicenseDetail.owners");
      owners = (owners && convertOwnerDobToEpoch(owners)) || [];
  
      //set(queryObject[0], "tradeLicenseDetail.owners", getMultipleOwners(owners));
      const cityId = get(
        queryObject[0],
        "tradeLicenseDetail.address.tenantId",
        ""
      );
      const tenantId = ifUserRoleExists("CITIZEN") ? cityId : getTenantId();
      const BSqueryObject = [
        { key: "tenantId", value: tenantId },
        { key: "businessService", value: "newTL" }
      ];
      if (process.env.REACT_APP_NAME === "Citizen") {
        let currentFinancialYr = getCurrentFinancialYear();
        //Changing the format of FY
        let fY1 = currentFinancialYr.split("-")[1];
        fY1 = fY1.substring(2, 4);
        currentFinancialYr = currentFinancialYr.split("-")[0] + "-" + fY1;
        set(queryObject[0], "financialYear", currentFinancialYr);
        setBusinessServiceDataToLocalStorage(BSqueryObject, dispatch);
      }
  
      set(queryObject[0], "tenantId", tenantId);
  
      if (queryObject[0].applicationNumber) {
        //call update
  
        let accessories = get(queryObject[0], "tradeLicenseDetail.accessories");
        let tradeUnits = get(queryObject[0], "tradeLicenseDetail.tradeUnits");
        set(
          queryObject[0],
          "tradeLicenseDetail.tradeUnits",
          getMultiUnits(tradeUnits)
        );
        set(
          queryObject[0],
          "tradeLicenseDetail.accessories",
          getMultiUnits(accessories)
        );
        set(
          queryObject[0],
          "tradeLicenseDetail.owners",
          getMultipleOwners(owners)
        );
  
        let action = "INITIATE";
        if (
          queryObject[0].tradeLicenseDetail &&
          queryObject[0].tradeLicenseDetail.applicationDocuments
        ) {
          if (getQueryArg(window.location.href, "action") === "edit") {
            // const removedDocs = get(
            //   state.screenConfiguration.preparedFinalObject,
            //   "LicensesTemp[0].removedDocs",
            //   []
            // );
            // set(queryObject[0], "tradeLicenseDetail.applicationDocuments", [
            //   ...get(
            //     state.screenConfiguration.prepareFinalObject,
            //     "Licenses[0].tradeLicenseDetail.applicationDocuments",
            //     []
            //   ),
            //   ...removedDocs
            // ]);
          } else if (activeIndex === 1) {
            alert("active index 1");
  
            set(queryObject[0], "tradeLicenseDetail.applicationDocuments", null);
          } else action = "APPLY";
        }
        // else if (
        //   queryObject[0].tradeLicenseDetail &&
        //   queryObject[0].tradeLicenseDetail.applicationDocuments &&
        //   activeIndex === 1
        // ) {
        // } else if (
        //   queryObject[0].tradeLicenseDetail &&
        //   queryObject[0].tradeLicenseDetail.applicationDocuments
        // ) {
        //   action = "APPLY";
        // }
        set(queryObject[0], "action", action);
        const isEditFlow = getQueryArg(window.location.href, "action") === "edit";
        !isEditFlow &&
          (await httpRequest("post", "/tl-services/v1/_update", "", [], {
            Licenses: queryObject
          }));
        let searchQueryObject = [
          { key: "tenantId", value: queryObject[0].tenantId },
          { key: "applicationNumber", value: queryObject[0].applicationNumber }
        ];
        let searchResponse = await getSearchResults(searchQueryObject);
        if (isEditFlow) {
          searchResponse = { Licenses: queryObject };
        } else {
          dispatch(prepareFinalObject("Licenses", searchResponse.Licenses));
        }
        const updatedtradeUnits = get(
          searchResponse,
          "Licenses[0].tradeLicenseDetail.tradeUnits"
        );
        const tradeTemp = updatedtradeUnits.map((item, index) => {
          return {
            tradeSubType: item.tradeType.split(".")[1],
            tradeType: item.tradeType.split(".")[0]
          };
        });
        dispatch(prepareFinalObject("LicensesTemp.tradeUnits", tradeTemp));
        createOwnersBackup(dispatch, searchResponse);
      } else {
        let accessories = get(queryObject[0], "tradeLicenseDetail.accessories");
        let tradeUnits = get(queryObject[0], "tradeLicenseDetail.tradeUnits");
        // let owners = get(queryObject[0], "tradeLicenseDetail.owners");
        let mergedTradeUnits =
          tradeUnits &&
          tradeUnits.filter(item => !item.hasOwnProperty("isDeleted"));
        let mergedAccessories =
          accessories &&
          accessories.filter(item => !item.hasOwnProperty("isDeleted"));
        let mergedOwners =
          owners && owners.filter(item => !item.hasOwnProperty("isDeleted"));
  
        set(queryObject[0], "tradeLicenseDetail.tradeUnits", mergedTradeUnits);
        set(queryObject[0], "tradeLicenseDetail.accessories", mergedAccessories);
        set(queryObject[0], "tradeLicenseDetail.owners", mergedOwners);
        set(queryObject[0], "action", "INITIATE");
        //Emptying application docs to "INITIATE" form in case of search and fill from old TL Id.
        if (!queryObject[0].applicationNumber)
          set(queryObject[0], "tradeLicenseDetail.applicationDocuments", null);
        const response = await httpRequest(
          "post",
          "/tl-services/v1/_create",
          "",
          [],
          { Licenses: queryObject }
        );
        dispatch(prepareFinalObject("Licenses", response.Licenses));
        createOwnersBackup(dispatch, response);
      }
      /** Application no. box setting */
      setApplicationNumberBox(state, dispatch);
      return true;
    } catch (error) {
      dispatch(toggleSnackbar(true, { labelName: error.message }, "error"));
      console.log(error);
      return false;
    }
  };
  
  const convertOwnerDobToEpoch = owners => {
    let updatedOwners =
      owners &&
      owners
        .map(owner => {
          return {
            ...owner,
            dob:
              owner && owner !== null && convertDateToEpoch(owner.dob, "dayend")
          };
        })
        .filter(item => item && item !== null);
    return updatedOwners;
  };
  
  export const getImageUrlByFile = file => {
    return new Promise(resolve => {
      var reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = e => {
        const fileurl = e.target.result;
        resolve(fileurl);
      };
    });
  };
  
  export const getFileSize = file => {
    const size = parseFloat(file.size / 1024).toFixed(2);
    return size;
  };
  
  export const isFileValid = (file, acceptedFiles) => {
    const mimeType = file["type"];
    return (
      (mimeType &&
        acceptedFiles &&
        acceptedFiles.indexOf(mimeType.split("/")[1]) > -1) ||
      false
    );
  };
  
  const setApplicationNumberBox = (state, dispatch) => {
    let applicationNumber = get(
      state,
      "screenConfiguration.preparedFinalObject.Licenses[0].applicationNumber",
      null
    );
    if (applicationNumber) {
      dispatch(
        handleField(
          "apply",
          "components.div.children.headerDiv.children.header.children.applicationNumber",
          "visible",
          true
        )
      );
      dispatch(
        handleField(
          "apply",
          "components.div.children.headerDiv.children.header.children.applicationNumber",
          "props.number",
          applicationNumber
        )
      );
    }
  };
  
  export const findItemInArrayOfObject = (arr, conditionCheckerFn) => {
    for (let i = 0; i < arr.length; i++) {
      if (conditionCheckerFn(arr[i])) {
        return arr[i];
      }
    }
  };
  