/*
 This file is part of GNU Anastasis
 (C) 2021-2022 Anastasis SARL
 GNU Anastasis is free software; you can redistribute it and/or modify it under the
 terms of the GNU Affero General Public License as published by the Free Software
 Foundation; either version 3, or (at your option) any later version.
 GNU Anastasis is distributed in the hope that it will be useful, but WITHOUT ANY
 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
 You should have received a copy of the GNU Affero General Public License along with
 GNU Anastasis; see the file COPYING.  If not, see 
no reducer in context
;
  }
  if (
    !reducer.currentReducerState ||
    reducer.currentReducerState.reducer_type !== "recovery"
  ) {
    return invalid state
;
  }
  return (
    
      
        
          Found versions:
          {policies.map((version, i) => (
            
              
                
                  
                    Name:  {version.secret_name} 
                  
                  
                    Id:  
                    
                       
                    {version.policy_hash.substring(0, 22)}... 
                  
                 
                
                   onNext(version)}
                  >
                    Recover
                   
                 
               
             
          ))}
        
 
        
       
     
  );
}
export function SecretSelectionScreen(): VNode {
  const reducer = useAnastasisContext();
  const [manageProvider, setManageProvider] = useState(false);
  useEffect(() => {
    async function f() {
      if (reducer) {
        await reducer.discoverStart();
      }
    }
    f().catch((e) => console.log(e));
  }, []);
  if (!reducer) {
    return no reducer in context
;
  }
  if (
    !reducer.currentReducerState ||
    reducer.currentReducerState.reducer_type !== "recovery"
  ) {
    return invalid state
;
  }
  if (manageProvider) {
    return (
       setManageProvider(false)} />
    );
  }
  if (
    reducer.discoveryState.state === "none" ||
    reducer.discoveryState.state === "active"
  ) {
    // Can this even happen?
    return  setManageProvider(false)}
        notifications={[
          {
            message: "Secret not found",
            type: "ERROR",
            description:
              "With the information you provided we could not found secret in any of the providers. You can try adding more providers if you think the data is correct.",
          },
        ]}
      />
    );
  }
  return (
     reducer.transition("select_version", version)}
      onManageProvider={async () => setManageProvider(false)}
    />
  );
}
// export function OldSecretSelectionScreen(): VNode {
//   const [selectingVersion, setSelectingVersion] = useState(false);
//   const reducer = useAnastasisContext();
//   const [manageProvider, setManageProvider] = useState(false);
//   useEffect(() => {
//     async function f() {
//       if (reducer) {
//         await reducer.discoverStart();
//       }
//     }
//     f().catch((e) => console.log(e));
//   }, []);
//   const currentVersion =
//     (reducer?.currentReducerState &&
//       "recovery_document" in reducer.currentReducerState &&
//       reducer.currentReducerState.recovery_document?.version) ||
//     0;
//   if (!reducer) {
//     return no reducer in context
;
//   }
//   if (
//     !reducer.currentReducerState ||
//     reducer.currentReducerState.reducer_type !== "recovery"
//   ) {
//     return invalid state
;
//   }
//   async function doSelectVersion(p: string, n: number): Promise {
//     if (!reducer) return Promise.resolve();
//     return reducer.runTransaction(async (tx) => {
//       await tx.transition("select_version", {
//         version: n,
//         provider_url: p,
//       });
//       setSelectingVersion(false);
//     });
//   }
//   const provs = reducer.currentReducerState.authentication_providers ?? {};
//   const recoveryDocument = reducer.currentReducerState.recovery_document;
//   if (!recoveryDocument) {
//     return (
//        doSelectVersion(newProv, 0)}
//       />
//     );
//   }
//   if (selectingVersion) {
//     return (
//        setSelectingVersion(false)}
//         onConfirm={doSelectVersion}
//       />
//     );
//   }
//   if (manageProvider) {
//     return (
//        setManageProvider(false)} />
//     );
//   }
//   const providerInfo = provs[
//     recoveryDocument.provider_url
//   ] as AuthenticationProviderStatusOk;
//   return (
//     
//       
//         
//           
//             
{providerInfo.business_name} 
//             
//               {currentVersion === 0 ? (
//                 
Set to recover the latest version
//               ) : (
//                 
Set to recover the version number {currentVersion}
//               )}
//             
//             
//                setSelectingVersion(true)}>
//                 Change secret's version
//                
//             
//           
//         
//         
//       
 
//   );
// }
function ChooseAnotherProviderScreen({
  onChange,
}: {
  onChange: (prov: string) => void;
}): VNode {
  const reducer = useAnastasisContext();
  if (!reducer) {
    return no reducer in context
;
  }
  if (
    !reducer.currentReducerState ||
    reducer.currentReducerState.reducer_type !== "recovery"
  ) {
    return invalid state
;
  }
  const providers = reducer.currentReducerState.authentication_providers ?? {};
  return (
    
      No recovery document found, try with another provider
      
        Provider 
        
          
             onChange(e.currentTarget.value)} value="">
              
                Choose a provider
               
              {Object.keys(providers).map((url) => {
                const p = providers[url];
                if (!("methods" in p)) return null;
                return (
                  
                    {p.business_name}
                   
                );
              })}
             
            
              
           
         
       
     
  );
}
function SelectOtherVersionProviderScreen({
  providers,
  provider,
  version,
  onConfirm,
  onCancel,
}: {
  onCancel: () => void;
  provider: string;
  version: number;
  providers: { [url: string]: AuthenticationProviderStatus };
  onConfirm: (prov: string, v: number) => Promise;
}): VNode {
  const [otherProvider, setOtherProvider] = useState(provider);
  const [otherVersion, setOtherVersion] = useState(
    version > 0 ? String(version) : "",
  );
  const otherProviderInfo = providers[
    otherProvider
  ] as AuthenticationProviderStatusOk;
  return (
    
      
        
          
            Provider {otherProviderInfo.business_name} 
            
              {version === 0 ? (
                
Set to recover the latest version
              ) : (
                
Set to recover the version number {version}
              )}
              
Specify other version below or use the latest
            
              Provider 
              
                
                   setOtherProvider(e.currentTarget.value)}
                    value={otherProvider}
                  >
                    
                      {" "}
                      Choose a provider{" "}
                     
                    {Object.keys(providers).map((url) => {
                      const p = providers[url];
                      if (!("methods" in p)) return null;
                      return (
                        
                          {p.business_name}
                         
                      );
                    })}
                   
                  
                    
                 
               
             
            
           
          
         
       
     
  );
}
function SecretSelectionScreenWaiting(): VNode {
  return (
    
      loading secret versions
     
  );
}