// Available variables:
  // - Machine
  // - interpret
  // - assign
  // - send
  // - sendParent
  // - spawn
  // - raise
  // - actions
  // - XState (all XState exports)
  
  const goToEvents = {
    goToProfileInformation: 'fillOutProfileInformation',
    goToProductsBrands: 'productsAndBrands',
    goToServiceAreas: 'serviceAreas',
    goToScheduleAvailability: 'scheduleAvailability',
    goToSetUpTechnicians: 'setUpTechnicians'
  }

  const M = Machine({
    id: 'sbxui',
    initial: '_',
    context: {
      authenticated: false,
      signedUp: false,
    },
    on: {
      linkToTermsAndConditions: '',
      linkToPrivacyPolicy: '',
      clickLogoutButton: {
        target: 'landingPage',
        cond: ctx => ctx.authenticated,
        actions: assign({authenticated: false})
      }
    },
    states: {
      _: {
        on: {
          'Navigate to landing page': 'landingPage',
          'Navigate to login page': 'login',
          'Invite via email for existing SPs': 'login',
          'Navigate to sign-up': 'fillOutProfileInformation',
          'Link via authenticated SB.com session': 'singleClickSigup',
          'Link via authenticated SB.com session for user with BMS/Self-dispatch': {target: 'productsAndBrands', actions: assign({signedUp: true})}
        },
      },
      landingPage: {
        on: {
          goToLogin: 'login',
          goToSignUp: 'fillOutProfileInformation'
        }
      },
      login: {
        on: {
          clickSignUpButton: 'fillOutProfileInformation',
        },
        initial: 'fillOutForm',
          states: {
            fillOutForm: {
              on: {
                submitLoginButton: 'busyAuthenticating'
              }
            },
            busyAuthenticating: {
              on: {
                                authenticatedAsReturningBmsUser: {
                  target: '#sbxui.productsAndBrands',
                  actions: assign({ authenticated: true, signedUp: true })
                },
                authenticatedAsServiceProviderWithNoBMS: {
                  target: '#sbxui.singleClickSigup',
                  actions: assign({ authenticated: true })
                }
              }
            },
          }
      },
      fillOutProfileInformation: {
        initial: 'accountInformation',
        on: {
          CONTINUE: [
            { target: 'acceptEula', cond: ctx => !ctx.signedUp },
            { target: 'premiumServices' },
          ],
        },
        states: {
          accountInformation: {
            on: {
              '': {
                target: 'securityQuestions',
                cond: ctx => ctx.authenticated,
              },
              goToSecurityQuestions: 'securityQuestions',
              goToContactInformation: 'contactInformation',
              goToCompanyAddres: 'companyAddress'
            }
          },
          securityQuestions: {
            on: {
              goToAccountInformation: {
                target: 'accountInformation',
                cond: ctx => !ctx.authenticated,
              },
              goToContactInformation: 'contactInformation',
              goToCompanyAddres: 'companyAddress'
            }
          },
          contactInformation: {
            on: {
              goToAccountInformation: {
                target: 'accountInformation',
                cond: ctx => !ctx.authenticated,
              },
              goToSecurityQuestions: 'securityQuestions',
              goToCompanyAddres: 'companyAddress'
            }
          },
          companyAddress: {
            on: {
              goToAccountInformation: {
                target: 'accountInformation',
                cond: ctx => !ctx.authenticated,
              },
              goToSecurityQuestions: 'securityQuestions',
              goToContactInformation: 'contactInformation'
            }
          },
        }
      },
      singleClickSigup: {
        on: {
          YES: 'acceptEula',
          NO: 'productsAndBrands'
        }
      },
      acceptEula: {
        on: {
          ACCEPT: {
            target: 'premiumServices',
            actions: assign({
              signedUp: true,
              authenticated: true
            })
          },
          DECLINE: 'declinedEULA'
        }
      },
      premiumServices: {
        on: goToEvents,
      },
      productsAndBrands: {
        on: goToEvents,
      },
      serviceAreas: {
        on: goToEvents,
      },
      scheduleAvailability: {
        on: goToEvents,
      },
      setUpTechnicians: {
        on: goToEvents,
      },
      confirmationPage: {},
      declinedEULA: {
        after: {
          1000: {
            actions: send('clickLogoutButton')
          }
        }
      },
    },
    
  });