renderView:
  - load: message_bubbles

  - type: Phantom
    actions:
      - what: hide
        when: ~~.__state.currentScreen
        isNot: screen__onboarding_config_method
    content:
      load: screen__onboarding_config_method

  - type: Phantom
    actions:
      - what: hide
        when: ~~.__state.currentScreen
        isNot: screen__onboarding_1
    content:
      load: screen__onboarding_1

  - type: Phantom
    actions:
      - what: hide
        when: ~~.__state.currentScreen
        isNot: screen__main
    content:
      load: screen__main

  - type: Phantom
    actions:
      - what: hide
        when: ~~.__state.currentScreen
        isNot: screen__tracking
    content:
      load: screen__tracking

  - type: Phantom
    actions:
      - what: redirect
        to: ~~.__redirectTo
        when: ~~.__redirectTo
        isEmpty: not

  - type: Phantom
    actions:
      - what: setData
        on: message
        whenMessageIs:
          type: queue-processed
        path: ~~.__state.queueProcessed
        value: true

templates:

  message_bubbles:
    type: Switch
    cardinality: -1
    content: ~~.__message
    options:
      error:
        load: message_bubble_error
      info:
        load: message_bubble_info
      warning:
        load: message_bubble_warning

  message_bubble_error:
    type: div
    attributes:
      class: notice notice-error is-dismissible p-3
    content: ~.text

  message_bubble_info:
    type: div
    attributes:
      class: notice notice-info is-dismissible p-3
    content: ~.text

  message_bubble_warning:
    type: div
    attributes:
      class: notice notice-warning is-dismissible p-3
    content: ~.text

  screen__main:
    - type: section
      attributes:
        class: card wpd-card
      content:

        - type: h2
          content: About this instance

        - type: Phantom
          actions:
            - what: hide
              when: ~~.settings.isProd
              isNot: "1"
          content:
            - type: div
              attributes: { class: card-item }
              content: This is your production instance.

            - type: div
              attributes: { class: card-item }
              actions:
                - what: hide
                  when: ~~.settings.areEndpointsOpened
                  isNot: "0"
              content:
                - type: div
                  attributes:
                    style: { marginBottom: 0.5em }
                  content:
                    - "Interactions from development instances are "
                    - type: strong
                      content: disallowed
                    - "."
                - type: ul
                  content:
                    - type: li
                      content: You will not be able to set up a new development instance.
            #                    - type: li
            #                      content: The in-editor synchronization will not work.
            #                    - type: li
            #                      content: Diff checks from development instances will not work.

            - type: div
              attributes: { class: card-item }
              actions:
                - what: hide
                  when: ~~.settings.areEndpointsOpened
                  isNot: "1"
              content:
                - "Interactions from development instances are "
                - type: strong
                  content: allowed
                - "."

            - type: div
              attributes: { class: card-item }
              actions:
                - what: hide
                  when: ~~.settings.allowsSynchronizations
                  isNot: "0"
              content:
                - "Synchronizations from development instances are "
                - type: strong
                  content: disallowed
                - "."

            - type: div
              attributes: { class: card-item }
              actions:
                - what: hide
                  when: ~~.settings.allowsSynchronizations
                  isNot: "1"
              content:
                - "Synchronizations from development instances are "
                - type: strong
                  content: allowed
                - "."

            - type: div
              attributes:
                class: card-item d-flex align-items-center gap-2
              content:
                - You will need to copy the access token to allow
                  your dev instances to interact with this instance.
                - type: div
                  content:
                    - type: BsButton
                      content: 📋 Copy
                      attributes:
                        style:
                          textWrap: nowrap
                      actions:
                        - what: setClipboardData
                          value: ~~.settings.accessToken
                          on: click

        - type: Phantom
          actions:
            - what: hide
              when: ~~.settings.isProd
              isNot: "0"
          content:

            - type: div
              attributes: { class: card-item }
              content: This is your development instance.

            - type: div
              attributes: { class: card-item }
              content:
                - "Your production instance is "
                - type: strong
                  attributes: { class: text-nowrap }
                  content: ~~.settings.prodUrl
                - "."

    - type: Phantom
      actions:
        - what: hide
          when: ~~.settings.isProd
          isNot: "0"
      content:
        - # Synchronization section.
          type: section
          attributes:
            class: card wpd-card
          content:

            - type: h2
              content: Synchronize

            - load: startSynchronizeButton

  screen__onboarding_config_method:
    type: div
    attributes:
      class: d-flex flex-column gap-3 justify-content-center text-center
      style:
        margin: auto
        width: 600px
    content:
      - type: div
        content:
          - "Thank you for installing "
          - type: em
            content: Deploy & Sync Content!

      - type: div
        content: You are only a few steps away from easy synchronizations between your environments!

      - type: div
        content:
          This is a new install that needs to be configured to work. EA Lab provides an online service
          that will configure automatically Deploy & Sync Content. When using this online service, a few
          metadata of this install will be sent to the EA Lab server. Such metadata is required so that
          the provided configuration matches your install, and to protect our systems against spamming and
          malicious behaviours.

      - type: div
        content:
          If you don't want to connect to the EA Lab server, you can opt for a manual configuration. In that
          case, you will have to configure Deploy & Sync Content either by using a 3rd-party
          configuration provider, or by yourself (for advanced users).

      - type: div
        content: "Select your configuration method:"

      - type: CheckBoxField
        dataLocation: ~~.settings.configuration_method
        controlType: "radio"
        options:
          - label: 
              - type: div
                attributes:
                  style:
                    fontWeight: "500"
                content: "Automatic configuration (recommended)"
              - type: div
                attributes:
                  style:
                    fontSize: "0.875rem"
                    color: "#6c757d"
                    marginTop: "0.5rem"
                    lineHeight: "1.4"
                content:
                  - "By selecting this option, I agree to the sending of metadata to EA Lab's service in accordance with the "
                  - type: a
                    content: "terms of use"
                    attributes:
                      href: "https://wp-deployer.io/en/general-terms-and-conditions-use-gtcu"
                      target: "_blank"
                      rel: "noopener noreferrer"
                  - "."
            value: "auto"
            attributes:
              style:
                border: "1px solid #d4edda"
                borderRadius: "5px"
                backgroundColor: "#f8fff9"
                padding: "1rem"
                paddingLeft: "3rem"
                margin: "0.5rem 0"
                cursor: "pointer"
                textAlign: "left"
                transition: "all 0.2s ease"
              className: "config-option-card"
          - label: 
              - type: div
                attributes:
                  style:
                    fontWeight: "500"
                content: "Manual configuration"
              - type: div
                attributes:
                  style:
                    fontSize: "0.875rem"
                    color: "#6c757d"
                    marginTop: "0.5rem"
                    lineHeight: "1.4"
                content: "Configure Deploy & Sync Content manually or using a 3rd-party provider."
            value: "manual"
            attributes:
              style:
                border: "1px solid #d1ecf1"
                borderRadius: "5px"
                backgroundColor: "#f8f9ff"
                padding: "1rem"
                paddingLeft: "3rem"
                margin: "0.5rem 0"
                cursor: "pointer"
                textAlign: "left"
                transition: "all 0.2s ease"
              className: "config-option-card"

      # Bouton désactivé (affiché quand aucune option n'est sélectionnée)
      - type: BsButton
        content: Continue
        attributes:
          disabled: true
          class: "btn-secondary"
        actions:
          - what: hide
            when: ~~.settings.configuration_method
            isEmpty: not
      
      # Bouton actif (affiché quand une option est sélectionnée)
      - type: BsButton
        content: Continue
        attributes:
          class: "btn-primary"
        actions:
          - what: hide
            when: ~~.settings.configuration_method
            isEmpty:
          - what: submitData
            on: click
            url: "/wp-json/deploy-sync-content/v1/dashboard/onboarding/config-method"

  screen__onboarding_1:
    type: div
    attributes:
      class: d-flex flex-column gap-3 justify-content-center text-center
      style:
        margin: auto
        width: 600px
    content:
      - type: div
        content:
          To set up things properly, we need a few info about your WordPress install.

      - type: div
        attributes:
          class: d-flex flex-column gap-3
        content:
          - type: div
            content:
              - type: div
                content: Is this your production instance?
              - type: div
                attributes:
                  class: text-muted
                content: (Your production instance is your publicly available website.)

          - type: div
            attributes:
              class: d-flex justify-content-center
            content:
              type: CheckBoxField
              controlType: radio
              options:
                - label: Yes
                  value: "1"
                  attributes:
                    inline: inline
                - label: No
                  value: "0"
                  attributes:
                    inline: inline
              dataLocation: ~~.__state.isProd

      - type: div
        attributes:
          style:
            marginTop: 50px
        actions:
          - what: hide
            when: ~~.__state.isProd
            isNot: "0"
        content:
          - type: TextField
            label: What is your production instance URL?
            placeholder: https://your-production-site.example
            dataLocation: ~~.__state.prodUrl
          - type: div
            content:
              - "This instance must have "
              - type: em
                content: Deploy & Sync Content
              - " already installed and working."

      - type: div
        actions:
          - what: hide
            when: ~~.__state.isProd
            isNot: "0"
        attributes:
          style:
            marginTop: 50px
        content:
          - type: div
            content:
              - "A token is required by "
              - type: em
                content: Deploy & Sync Content
              - " to allow communication between this instance and your production instance.
                This token can be found on the "
              - type: em
                content: Deploy & Sync Content
              - " dashboard page of your production instance."
          - type: TextField
            dataLocation: ~~.__state.customToken
            placeholder: Paste your custom token here.


      - type: div
        attributes:
          class: d-flex justify-content-center
        content:
          type: BsButton
          content: Continue
          actions:
            - what: hide
              orConditions:
                - # Hide when not explicitly set as prod.
                  when: ~~.__state.isProd
                  isEmpty:
                - # Hide when non-prod, and URL is missing.
                  andConditions:
                    - when: ~~.__state.isProd
                      is: "0"
                    - when: ~~.__state.prodUrl
                      isEmpty:
                - # Hide when a custom token must be supplied, but is missing.
                  andConditions:
                    - when: ~~.__state.isProd
                      is: "0"
                    - when: ~~.__state.customToken
                      isEmpty:
            - what: submitData
              on: click
              url: "/wp-json/deploy-sync-content/v1/dashboard/onboarding/1"

      - type: div
        attributes:
          class: d-flex justify-content-center
        content:
          type: BsButton
          attributes:
            disabled: disabled
          content: Continue
          actions:
            - what: hide
              orConditions:
                - # Hide when explicitly set as prod.
                  when: ~~.__state.isProd
                  is: "1"
                - # Hide when non-prod, and URL is set.
                  andConditions:
                    - when: ~~.__state.isProd
                      is: "0"
                    - when: ~~.__state.prodUrl
                      isEmpty: not
                    - when: ~~.__state.customToken
                      isEmpty: not

  screen__tracking:
    type: div
    attributes:
      class: d-flex flex-column gap-3 justify-content-center
    content:
      - type: div
        attributes:
          class: d-flex justify-content-center gap-3
        content:
          - type: div
            actions:
              - what: hide
                when: ~~.__state.queueProcessed
                is: true
            attributes:
              class: text-center
            content:
              - We are preparing this instance.
              - type: br
              - Depending on the size of the content on this instance, it may take some time.
              - type: br
              - You can safely close this window or navigate, and check later.

          - type: div
            attributes:
              class: d-flex flex-column gap-3
            actions:
              - what: hide
                when: ~~.__state.queueProcessed
                isNot: true
            content:
              - type: div
                attributes:
                  class: d-flex justify-content-center gap-3
                content: Your instance is ready! We wish you happy synchronizations!

              - type: div
                actions:
                  - what: hide
                    when: ~~.settings.isProd
                    isNot: "1"
                  - what: setData
                    on: click
                    path: ~~.__state.currentScreen
                    value: screen__main
                attributes:
                  class: d-flex justify-content-center gap-3
                content:
                  type: BsButton
                  content: Go to the dashboard

              - type: div
                actions:
                  - what: hide
                    when: ~~.settings.isProd
                    isNot: "0"
                attributes:
                  class: d-flex justify-content-center gap-3
                content:
                  load: startSynchronizeButton

      - type: div
        content:
          type: BsAccordion
          attributes:
            style:
              margin: auto
              maxWidth: 1000px
          content:
            type: AccordionItem
            header: See progress
            body:
              type: iframe
              attributes:
                src: ~~.trackingQueueProgressSrc
                style:
                  height: 100vh
                  width: 100%

  startSynchronizeButton:
    type: div
    attributes:
      class: d-flex flex-column gap-3
    content:
      - type: div
        attributes:
          class: d-flex justify-content-center
        content:
          - type: BsButton
            content: View items to synchronize
            actions:
              - what: submitData
                on: click
                url: "/wp-json/deploy-sync-content/v1/dashboard/synchronize"
              - what: hide
                when: ~~.__redirectTo
                isEmpty: not
          - type: BsButton
            attributes: { disabled: disabled }
            content: View items to synchronize
            actions:
              - what: hide
                when: ~~.__redirectTo
                isEmpty:

      - type: div
        attributes:
          class: d-flex justify-content-center
        content:
          - type: div
            actions:
              - what: hide
                when: ~~.__redirectTo
                isEmpty:
            content:
              - type: div
                content: The synchronization content has been downloaded. Please wait...
              - type: DelayedActions
                interval: 10000
                once: true
                delayedActions:
                  - what: setData
                    path: ~~.showRedirectionLink
                    value: true
              - # Fallback when the redirect failed for some reason.
                type: a
                actions:
                  - what: hide
                    when: ~~.showRedirectionLink
                    isEmpty:
                attributes:
                  href: ~~.__redirectTo
                content: Click here to open the synchronization page.

data:

  __state:
    currentScreen: screen__onboarding_1
    customToken:
    isProd:
    prodUrl:
    useDefaultToken: true

  # Real data coming from the plugin settings.
  # Do not confuse with the __state values.
  settings:
    accessToken:
    allowsSynchronizations:
    areEndpointsOpened:
    isProd:
    prodUrl:

  # Source URL for the tracking queue progress.
  trackingQueueProgressSrc:
