// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Cx from "rescript-classnames/src/Cx.bs.js";
import * as Curry from "rescript/lib/es6/curry.js";
import * as Icon3 from "../../Icon3.bs.js";
import * as React from "react";
import * as Js_dict from "rescript/lib/es6/js_dict.js";
import * as Caml_obj from "rescript/lib/es6/caml_obj.js";
import * as Belt_Array from "rescript/lib/es6/belt_Array.js";
import * as Caml_array from "rescript/lib/es6/caml_array.js";
import * as Belt_Option from "rescript/lib/es6/belt_Option.js";
import * as ImportFunctionsJs from "./importFunctions.js";

function importSheet(prim0, prim1) {
  return ImportFunctionsJs.importSheet(prim0, prim1);
}

function buildLocked(importData) {
  return Belt_Array.reduce(Belt_Array.keepMap(importData.sourceColumns, (function (item) {
                    var match = importData.schema.locate;
                    var match$1 = importData.schema.contacts;
                    if (match !== undefined) {
                      if (match$1 === undefined) {
                        if (Caml_obj.equal(match.addressKey, item)) {
                          return [
                                  item,
                                  "Address"
                                ];
                        } else if (Caml_obj.equal(match.cityKey, item)) {
                          return [
                                  item,
                                  "City"
                                ];
                        } else if (Caml_obj.equal(match.latitudeKey, item)) {
                          return [
                                  item,
                                  "Latitude"
                                ];
                        } else if (Caml_obj.equal(match.longitudeKey, item)) {
                          return [
                                  item,
                                  "Longitude"
                                ];
                        } else if (Caml_obj.equal(match.stateKey, item)) {
                          return [
                                  item,
                                  "State"
                                ];
                        } else if (Caml_obj.equal(match.zipKey, item)) {
                          return [
                                  item,
                                  "Zip"
                                ];
                        } else {
                          return ;
                        }
                      }
                      if (Caml_obj.equal(match.addressKey, item)) {
                        return [
                                item,
                                "Address"
                              ];
                      }
                      if (Caml_obj.equal(match.cityKey, item)) {
                        return [
                                item,
                                "City"
                              ];
                      }
                      if (Caml_obj.equal(match.latitudeKey, item)) {
                        return [
                                item,
                                "Latitude"
                              ];
                      }
                      if (Caml_obj.equal(match.longitudeKey, item)) {
                        return [
                                item,
                                "Longitude"
                              ];
                      }
                      if (Caml_obj.equal(match.stateKey, item)) {
                        return [
                                item,
                                "State"
                              ];
                      }
                      if (Caml_obj.equal(match.zipKey, item)) {
                        return [
                                item,
                                "Zip"
                              ];
                      }
                      var contactIndex = Belt_Array.getIndexBy(match$1, (function (contact) {
                              if (Caml_obj.equal(contact.contactNameKey, item) || Caml_obj.equal(contact.contactAddressKey, item) || Belt_Array.some(contact.contactPhoneKeys, (function (i) {
                                        return Caml_obj.equal(i, item);
                                      }))) {
                                return true;
                              } else {
                                return Belt_Array.some(contact.contactEmailKeys, (function (i) {
                                              return Caml_obj.equal(i, item);
                                            }));
                              }
                            }));
                      if (contactIndex === undefined) {
                        return ;
                      }
                      var contact = Caml_array.get(match$1, contactIndex);
                      if (Caml_obj.equal(contact.contactNameKey, item)) {
                        return [
                                item,
                                "Contact Name"
                              ];
                      }
                      if (Caml_obj.equal(contact.contactAddressKey, item)) {
                        return [
                                item,
                                "Contact Address"
                              ];
                      }
                      var phoneIndex = Belt_Array.getIndexBy(contact.contactPhoneKeys, (function (phone) {
                              return Caml_obj.equal(phone, item);
                            }));
                      var emailIndex = Belt_Array.getIndexBy(contact.contactEmailKeys, (function (email) {
                              return Caml_obj.equal(email, item);
                            }));
                      if (phoneIndex !== undefined) {
                        return [
                                item,
                                "Contact Phone " + String(((contactIndex << 1) + phoneIndex | 0) + 1 | 0) + ""
                              ];
                      } else if (emailIndex !== undefined) {
                        return [
                                item,
                                "Contact Email " + String(((contactIndex << 1) + emailIndex | 0) + 1 | 0) + ""
                              ];
                      } else {
                        return ;
                      }
                    }
                    if (match$1 === undefined) {
                      return ;
                    }
                    var contact$1 = Belt_Array.getBy(match$1, (function (contact) {
                            if (Caml_obj.equal(contact.contactNameKey, item) || Caml_obj.equal(contact.contactAddressKey, item) || Belt_Array.some(contact.contactPhoneKeys, (function (i) {
                                      return Caml_obj.equal(i, item);
                                    }))) {
                              return true;
                            } else {
                              return Belt_Array.some(contact.contactEmailKeys, (function (i) {
                                            return Caml_obj.equal(i, item);
                                          }));
                            }
                          }));
                    if (contact$1 === undefined) {
                      return ;
                    }
                    if (Caml_obj.equal(contact$1.contactNameKey, item)) {
                      return [
                              item,
                              "Contact Name"
                            ];
                    }
                    if (Caml_obj.equal(contact$1.contactAddressKey, item)) {
                      return [
                              item,
                              "Contact Address"
                            ];
                    }
                    var phoneIndex$1 = Belt_Array.getIndexBy(contact$1.contactPhoneKeys, (function (phone) {
                            return Caml_obj.equal(phone, item);
                          }));
                    var emailIndex$1 = Belt_Array.getIndexBy(contact$1.contactEmailKeys, (function (email) {
                            return Caml_obj.equal(email, item);
                          }));
                    if (phoneIndex$1 !== undefined) {
                      return [
                              item,
                              "Contact Phone " + String(phoneIndex$1 + 1 | 0) + ""
                            ];
                    } else if (emailIndex$1 !== undefined) {
                      return [
                              item,
                              "Contact Email " + String(emailIndex$1 + 1 | 0) + ""
                            ];
                    } else {
                      return ;
                    }
                  })), {}, (function (acc, param) {
                acc[param[0]] = param[1];
                return acc;
              }));
}

function buildMappings(importData) {
  return Belt_Array.reduce(Belt_Array.keep(importData.sourceColumns, (function (item) {
                    var match = importData.schema.locate;
                    var match$1 = importData.schema.contacts;
                    if (match !== undefined) {
                      if (match$1 !== undefined) {
                        return !(Caml_obj.equal(match.addressKey, item) || Caml_obj.equal(match.cityKey, item) || Caml_obj.equal(match.latitudeKey, item) || Caml_obj.equal(match.longitudeKey, item) || Caml_obj.equal(match.stateKey, item) || Caml_obj.equal(match.zipKey, item) || Belt_Array.some(match$1, (function (contact) {
                                        if (Caml_obj.equal(contact.contactNameKey, item) || Caml_obj.equal(contact.contactAddressKey, item) || Belt_Array.some(contact.contactPhoneKeys, (function (i) {
                                                  return Caml_obj.equal(i, item);
                                                }))) {
                                          return true;
                                        } else {
                                          return Belt_Array.some(contact.contactEmailKeys, (function (i) {
                                                        return Caml_obj.equal(i, item);
                                                      }));
                                        }
                                      })));
                      } else {
                        return !(Caml_obj.equal(match.addressKey, item) || Caml_obj.equal(match.cityKey, item) || Caml_obj.equal(match.latitudeKey, item) || Caml_obj.equal(match.longitudeKey, item) || Caml_obj.equal(match.stateKey, item) || Caml_obj.equal(match.zipKey, item));
                      }
                    } else if (match$1 !== undefined && Belt_Array.some(match$1, (function (contact) {
                              return Caml_obj.equal(contact.contactNameKey, item) || Caml_obj.equal(contact.contactAddressKey, item) || Belt_Array.some(contact.contactPhoneKeys, (function (i) {
                                            return Caml_obj.equal(i, item);
                                          })) ? true : Belt_Array.some(contact.contactEmailKeys, (function (i) {
                                              return Caml_obj.equal(i, item);
                                            }));
                            }))) {
                      return false;
                    } else {
                      return true;
                    }
                  })), {}, (function (acc, x) {
                acc[x] = undefined;
                return acc;
              }));
}

function Table__Import__Extract(Props) {
  var importData = Props.importData;
  var onComplete = Props.onComplete;
  var teamId = Props.teamId;
  var handleBack = Props.handleBack;
  var match = React.useState(function () {
        
      });
  var setError = match[1];
  var error = match[0];
  var match$1 = React.useState(function () {
        return false;
      });
  var setLoading = match$1[1];
  var loading = match$1[0];
  var match$2 = React.useState(function () {
        return buildMappings(importData);
      });
  var setMappings = match$2[1];
  var mappings = match$2[0];
  var match$3 = React.useState(function () {
        return buildLocked(importData);
      });
  return React.createElement("div", {
              className: "flex h-full w-full flex-col overflow-auto"
            }, React.createElement("div", {
                  className: "mt-4 text-2xl font-bold"
                }, "Map Remaining Columns"), React.createElement("div", {
                  className: "text-sm text-roman-silver"
                }, "Select the columns from your file and choose type"), React.createElement("div", {
                  className: "mt-4 flex flex-row justify-between"
                }, React.createElement("div", {
                      className: "text-xs text-roman-silver"
                    }, "FILE COLUMNS"), React.createElement("div", {
                      className: "mr-8 text-xs text-roman-silver"
                    }, "MAPSHEET COLUMNS")), React.createElement("div", {
                  className: "flex max-h-[30rem] w-full flex-col overflow-auto pr-2"
                }, Belt_Array.mapWithIndex(Js_dict.entries(match$3[0]), (function (index, param) {
                        return React.createElement("div", {
                                    key: String(index),
                                    className: "mt-2 flex w-full flex-row items-center"
                                  }, React.createElement("div", {
                                        className: "mr-4 flex flex-1 items-center justify-between rounded border border-gray-input bg-light-gray p-2 text-sm"
                                      }, param[0], React.createElement(Icon3.Lock.make, {})), React.createElement(Icon3.ArrowDoubleRight.make, {
                                        className: ""
                                      }), React.createElement("div", {
                                        className: "ml-4 flex flex-1 items-center justify-between rounded border border-gray-input bg-light-gray p-2 text-sm"
                                      }, param[1], React.createElement(Icon3.Lock.make, {})));
                      })), Belt_Array.mapWithIndex(Js_dict.entries(mappings), (function (index, param) {
                        var item = param[1];
                        var key = param[0];
                        return React.createElement("div", {
                                    key: String(index),
                                    className: "mt-2 flex w-full flex-row items-center"
                                  }, React.createElement("div", {
                                        className: "mr-4 flex flex-1 rounded border border-gray-input p-2 text-sm"
                                      }, key), React.createElement(Icon3.ArrowDoubleRight.make, {
                                        className: ""
                                      }), React.createElement("select", {
                                        className: "ml-4 flex flex-1 rounded border border-gray-input p-2 text-sm leading-none focus:border-gray",
                                        value: Belt_Option.getWithDefault(item, ""),
                                        onChange: (function ($$event) {
                                            var value = $$event.target.value;
                                            var newMappings = Js_dict.fromArray(Js_dict.entries(mappings));
                                            newMappings[key] = value;
                                            Curry._1(setMappings, (function (param) {
                                                    return newMappings;
                                                  }));
                                          })
                                      }, Belt_Array.concat([React.createElement("option", {
                                                  key: "0",
                                                  disabled: true,
                                                  selected: Belt_Option.isNone(item),
                                                  value: ""
                                                }, "Column Type")], Belt_Array.mapWithIndex(importData.targetColumns, (function (index, name) {
                                                  return React.createElement("option", {
                                                              key: String(index + 1 | 0),
                                                              value: name
                                                            }, name);
                                                })))));
                      }))), error !== undefined ? React.createElement("div", {
                    className: "mt-4 text-red"
                  }, error) : null, React.createElement("div", {
                  className: "flex flex-row justify-between"
                }, React.createElement("button", {
                      className: "my-6 mr-8",
                      onClick: (function (param) {
                          Curry._1(handleBack, importData);
                        })
                    }, React.createElement("div", {
                          className: "flex flex-row items-center justify-center rounded-16 py-4 px-14 hover:bg-light-gray"
                        }, React.createElement("div", undefined, "Back"))), React.createElement("button", {
                      className: "my-6",
                      disabled: loading,
                      onClick: (function (param) {
                          var entries = Js_dict.entries(mappings);
                          Curry._1(setError, (function (param) {
                                  
                                }));
                          Curry._1(setLoading, (function (param) {
                                  return true;
                                }));
                          var file = importData.file;
                          if (file === undefined) {
                            return Curry._1(setError, (function (param) {
                                          return "You must upload a file!";
                                        }));
                          }
                          var fields = Belt_Array.map(Belt_Array.keep(entries, (function (param) {
                                      return param[1] !== undefined;
                                    })), (function (param) {
                                  return {
                                          sourceKey: param[0],
                                          targetType: Belt_Option.getWithDefault(param[1], "")
                                        };
                                }));
                          var init = importData.schema;
                          var schema_targetTeamId = Belt_Option.getWithDefault(teamId, "");
                          var schema_maptableName = init.maptableName;
                          var schema_existingMaptableId = init.existingMaptableId;
                          var schema_source = init.source;
                          var schema_locate = init.locate;
                          var schema_contacts = init.contacts;
                          var schema_isTemplate = init.isTemplate;
                          var schema = {
                            targetTeamId: schema_targetTeamId,
                            maptableName: schema_maptableName,
                            existingMaptableId: schema_existingMaptableId,
                            source: schema_source,
                            locate: schema_locate,
                            contacts: schema_contacts,
                            fields: fields,
                            isTemplate: schema_isTemplate
                          };
                          ImportFunctionsJs.importSheet(file, schema).then(function (param) {
                                Curry._1(setMappings, (function (param) {
                                        return {};
                                      }));
                                Curry._1(setLoading, (function (param) {
                                        return false;
                                      }));
                                Curry._1(onComplete, undefined);
                                return Promise.resolve(undefined);
                              });
                        })
                    }, React.createElement("div", {
                          className: Cx.cx([
                                "flex w-40 flex-row items-center justify-center rounded-16 bg-blue",
                                loading ? "py-2" : "py-4"
                              ])
                        }, React.createElement("div", {
                              className: "text-white"
                            }, loading ? React.createElement("div", {
                                    className: "spinner"
                                  }) : "Complete Import")))));
}

var make = Table__Import__Extract;

export {
  importSheet ,
  buildLocked ,
  buildMappings ,
  make ,
}
/* Icon3 Not a pure module */
