{
  "id": "sun_moon_informer",
  "type": "module",
  "version": "1.2",
  "context": {
    "layout": "widget",
    "title": "Sun Moon Informer",
    "icon": "🌗"
  },
  "state": {
    "isLoading": true,
    "error": "",
    "latitude": 0,
    "longitude": 0,
    "sunrise": "",
    "sunset": "",
    "daylightSeconds": 0,
    "moonPhase": -1,
    "lastUpdated": "#{now}"
  },
  "dependencies": [
    {
      "repository": {
        "remote": {
          "id": "astro_api",
          "config": {
            "endPoint": "https://api.open-meteo.com/v1/forecast"
          }
        }
      }
    },
    {
      "service": {
        "location": {
          "id": "loc_service",
          "config": {}
        }
      }
    },
    {
      "service": {
        "audio": {
          "id": "audio"
        }
      }
    },
    {
      "action": {
        "state": {
          "id": "refresh_chain",
          "input": {
            "isLoading": true,
            "error": ""
          },
          "output": {
            "success": [
              {
                "service": {
                  "id": "refresh_location",
                  "input": {
                    "serviceId": "loc_service",
                    "params": {}
                  },
                  "output": {
                    "success": [
                      {
                        "repo": {
                          "id": "refresh_fetch",
                          "input": {
                            "repositoryId": "astro_api",
                            "operation": "read",
                            "params": {
                              "body": {
                                "latitude": "@{refresh_location.latitude}",
                                "longitude": "@{refresh_location.longitude}",
                                "daily": "sunrise,sunset,daylight_duration",
                                "timezone": "auto",
                                "forecast_days": 1
                              }
                            }
                          },
                          "output": {
                            "success": [
                              {
                                "state": {
                                  "id": "refresh_success",
                                  "input": {
                                    "isLoading": false,
                                    "error": "",
                                    "latitude": "@{refresh_location.latitude}",
                                    "longitude": "@{refresh_location.longitude}",
                                    "sunrise": "@{refresh_fetch.daily.sunrise[0]}",
                                    "sunset": "@{refresh_fetch.daily.sunset[0]}",
                                    "daylightSeconds": "@{refresh_fetch.daily.daylight_duration[0]}",
                                    "moonPhase": "{{ (((((cast(suffix(@{refresh_fetch.daily.time[0]}, 2), 'int') + cast(((153 * (cast(prefix(suffix(@{refresh_fetch.daily.time[0]}, 5), 2), 'int') + 12 * cast((14 - cast(prefix(suffix(@{refresh_fetch.daily.time[0]}, 5), 2), 'int')) / 12, 'int') - 3) + 2) / 5), 'int') + 365 * (cast(prefix(@{refresh_fetch.daily.time[0]}, 4), 'int') + 4800 - cast((14 - cast(prefix(suffix(@{refresh_fetch.daily.time[0]}, 5), 2), 'int')) / 12, 'int')) + cast((cast(prefix(@{refresh_fetch.daily.time[0]}, 4), 'int') + 4800 - cast((14 - cast(prefix(suffix(@{refresh_fetch.daily.time[0]}, 5), 2), 'int')) / 12, 'int')) / 4, 'int') - cast((cast(prefix(@{refresh_fetch.daily.time[0]}, 4), 'int') + 4800 - cast((14 - cast(prefix(suffix(@{refresh_fetch.daily.time[0]}, 5), 2), 'int')) / 12, 'int')) / 100, 'int') + cast((cast(prefix(@{refresh_fetch.daily.time[0]}, 4), 'int') + 4800 - cast((14 - cast(prefix(suffix(@{refresh_fetch.daily.time[0]}, 5), 2), 'int')) / 12, 'int')) / 400, 'int') - 32045) - 2451550.1) % 29.530588853) + 29.530588853) % 29.530588853) / 29.530588853 }}",
                                    "lastUpdated": "#{now}"
                                  }
                                }
                              }
                            ],
                            "failure": [
                              {
                                "state": {
                                  "id": "refresh_fetch_error",
                                  "input": {
                                    "isLoading": false,
                                    "error": "{{ 'Astronomy data could not load' + (notEmpty(@{refresh_fetch.message}) ? ': ' + @{refresh_fetch.message} : '. Check your connection and try again.') }}"
                                  }
                                }
                              }
                            ]
                          }
                        }
                      }
                    ],
                    "failure": [
                      {
                        "state": {
                          "id": "refresh_location_error",
                          "input": {
                            "isLoading": false,
                            "error": "{{ 'Location is needed for local sun times' + (notEmpty(@{refresh_location.message}) ? ': ' + @{refresh_location.message} : '. Allow location access and retry.') }}"
                          }
                        }
                      }
                    ]
                  }
                }
              }
            ]
          }
        }
      }
    }
  ],
  "events": {
    "onAppear": [
      {
        "template": {
          "id": "appear_refresh",
          "input": "refresh_chain"
        }
      }
    ]
  },
  "children": [
    {
      "id": "astro_tile",
      "type": "zstack",
      "style": {
        "layout": {
          "maxWidth": "infinity",
          "maxHeight": "infinity",
          "padding": 10
        },
        "common": {
          "background": {
            "gradient": {
              "linear": {
                "colors": [
                  "#111827",
                  "#243B6B",
                  "#F59E0B"
                ],
                "startPoint": "topLeading",
                "endPoint": "bottomTrailing"
              }
            }
          },
          "foreground": {
            "color": "white"
          },
          "cornerRadius": 28
        },
        "shadow": {
          "color": {
            "light": "#C7CBD8",
            "dark": "#000000"
          },
          "radius": 18,
          "x": 0,
          "y": 10
        },
        "animation": {
          "kind": "spring",
          "value": "${isLoading}"
        }
      },
      "children": [
        {
          "id": "load_gate",
          "type": "conditional",
          "context": {
            "_condition": "${isLoading}",
            "_trueComponent": {
              "id": "loading_card",
              "type": "vstack",
              "context": {
                "_spacing": 12,
                "_alignment": "center"
              },
              "style": {
                "layout": {
                  "maxWidth": "infinity",
                  "maxHeight": "infinity",
                  "padding": 20
                },
                "common": {
                  "foreground": {
                    "color": "white"
                  }
                }
              },
              "children": [
                {
                  "id": "loading_icon",
                  "type": "image",
                  "context": {
                    "_source": "sf://sparkles"
                  },
                  "style": {
                    "layout": {
                      "width": 34,
                      "height": 34
                    },
                    "common": {
                      "foreground": {
                        "color": "white"
                      },
                      "opacity": 0.9
                    }
                  }
                },
                {
                  "id": "loading_text",
                  "type": "text",
                  "context": {
                    "_text": "Reading the sky…"
                  },
                  "style": {
                    "common": {
                      "font": {
                        "kind": "headline",
                        "weight": "semibold"
                      },
                      "foreground": {
                        "color": "white"
                      }
                    }
                  }
                },
                {
                  "id": "loading_prog",
                  "type": "progress",
                  "style": {
                    "layout": {
                      "width": 130
                    },
                    "common": {
                      "tint": "white"
                    }
                  }
                },
                {
                  "id": "loading_hint",
                  "type": "text",
                  "context": {
                    "_text": "Getting your location and today’s sun times."
                  },
                  "style": {
                    "common": {
                      "font": "caption",
                      "foreground": {
                        "color": "white"
                      },
                      "opacity": 0.78
                    },
                    "text": {
                      "lineLimit": 2
                    }
                  }
                }
              ]
            },
            "_falseComponent": {
              "id": "ready_gate",
              "type": "conditional",
              "context": {
                "_condition": "{{ notEmpty(${error}) || empty(${sunrise}) || empty(${sunset}) }}",
                "_trueComponent": {
                  "id": "problem_card",
                  "type": "vstack",
                  "context": {
                    "_spacing": 12,
                    "_alignment": "center"
                  },
                  "style": {
                    "layout": {
                      "maxWidth": "infinity",
                      "maxHeight": "infinity",
                      "padding": 20
                    },
                    "common": {
                      "foreground": {
                        "color": "white"
                      }
                    }
                  },
                  "children": [
                    {
                      "id": "problem_icon",
                      "type": "image",
                      "context": {
                        "_source": "sf://exclamationmark.triangle.fill"
                      },
                      "style": {
                        "layout": {
                          "width": 34,
                          "height": 34
                        },
                        "common": {
                          "foreground": {
                            "color": "#FDE68A"
                          }
                        }
                      }
                    },
                    {
                      "id": "problem_text",
                      "type": "text",
                      "context": {
                        "_text": "{{ notEmpty(${error}) ? ${error} : 'No astronomy data came back for today. Try refreshing.' }}"
                      },
                      "style": {
                        "common": {
                          "font": "caption",
                          "foreground": {
                            "color": "white"
                          }
                        },
                        "text": {
                          "lineLimit": 3
                        }
                      }
                    },
                    {
                      "id": "retry_btn",
                      "type": "button",
                      "context": {
                        "_label": "Retry"
                      },
                      "style": {
                        "button": {
                          "standard": {
                            "type": "borderedProminent",
                            "shape": "capsule"
                          }
                        },
                        "layout": {
                          "padding": {
                            "top": 6,
                            "bottom": 6
                          }
                        },
                        "common": {
                          "tint": "#F59E0B"
                        }
                      },
                      "events": {
                        "onTap": [
                          {
                            "service": {
                              "id": "retry_tap_haptic",
                              "input": {
                                "serviceId": "audio",
                                "params": {
                                  "haptic": "light",
                                  "sound": "none"
                                }
                              }
                            }
                          },
                          {
                            "template": {
                              "id": "retry_refresh",
                              "input": "refresh_chain"
                            }
                          }
                        ]
                      }
                    }
                  ]
                },
                "_falseComponent": {
                  "id": "content_card",
                  "type": "vstack",
                  "context": {
                    "_spacing": 6,
                    "_alignment": "leading",
                    "hours": "{{ cast(${daylightSeconds} / 3600, 'int') }}",
                    "mins": "{{ cast((${daylightSeconds} % 3600) / 60, 'int') }}",
                    "daylightText": "{{ cast(@{hours}, 'str') + ' h ' + cast(@{mins}, 'str') + ' min' }}",
                    "moonName": "{{ switch(${moonPhase} < 0.03 || ${moonPhase} > 0.97, 'New Moon', ${moonPhase} < 0.22, 'Waxing Crescent', ${moonPhase} < 0.28, 'First Quarter', ${moonPhase} < 0.47, 'Waxing Gibbous', ${moonPhase} < 0.53, 'Full Moon', ${moonPhase} < 0.72, 'Waning Gibbous', ${moonPhase} < 0.78, 'Last Quarter', 'Waning Crescent') }}"
                  },
                  "style": {
                    "layout": {
                      "maxWidth": "infinity",
                      "maxHeight": "infinity",
                      "padding": 8
                    },
                    "common": {
                      "foreground": {
                        "color": "white"
                      }
                    }
                  },
                  "children": [
                    {
                      "id": "top_row",
                      "type": "hstack",
                      "context": {
                        "_alignment": "center",
                        "_spacing": 8
                      },
                      "style": {
                        "layout": {
                          "maxWidth": "infinity"
                        }
                      },
                      "children": [
                        {
                          "id": "loc_icon",
                          "type": "image",
                          "context": {
                            "_source": "sf://location.fill"
                          },
                          "style": {
                            "layout": {
                              "width": 12,
                              "height": 12
                            },
                            "common": {
                              "foreground": {
                                "color": "#FDE68A"
                              }
                            }
                          }
                        },
                        {
                          "id": "today_text",
                          "type": "text",
                          "context": {
                            "_text": "#{today}",
                            "_format": {
                              "date": {
                                "style": "date",
                                "dateStyle": "medium"
                              }
                            }
                          },
                          "style": {
                            "common": {
                              "font": "caption",
                              "foreground": {
                                "color": "white"
                              },
                              "opacity": 0.82
                            }
                          }
                        },
                        {
                          "id": "top_spacer",
                          "type": "spacer"
                        },
                        {
                          "id": "updated_text",
                          "type": "text",
                          "context": {
                            "_text": "${lastUpdated}",
                            "_format": {
                              "date": {
                                "style": "relative"
                              }
                            }
                          },
                          "style": {
                            "common": {
                              "font": "caption2",
                              "foreground": {
                                "color": "white"
                              },
                              "opacity": 0.7
                            }
                          }
                        }
                      ]
                    },
                    {
                      "id": "moon_row",
                      "type": "hstack",
                      "context": {
                        "_spacing": 8,
                        "_alignment": "center"
                      },
                      "style": {
                        "layout": {
                          "maxWidth": "infinity",
                          "padding": {
                            "top": 0,
                            "bottom": 0
                          }
                        }
                      },
                      "children": [
                        {
                          "id": "moon_badge",
                          "type": "zstack",
                          "style": {
                            "layout": {
                              "width": 44,
                              "height": 44
                            },
                            "common": {
                              "background": {
                                "color": "#EEF2FF"
                              },
                              "foreground": {
                                "color": "#1E1B4B"
                              },
                              "cornerRadius": 22
                            }
                          },
                          "children": [
                            {
                              "id": "moon_icon",
                              "type": "image",
                              "context": {
                                "_source": "sf://moon.fill"
                              },
                              "style": {
                                "layout": {
                                  "width": 26,
                                  "height": 26
                                },
                                "common": {
                                  "foreground": {
                                    "color": "#1E1B4B"
                                  }
                                }
                              }
                            }
                          ]
                        },
                        {
                          "id": "moon_label_stack",
                          "type": "vstack",
                          "context": {
                            "_spacing": 2,
                            "_alignment": "leading"
                          },
                          "children": [
                            {
                              "id": "moon_caption",
                              "type": "text",
                              "context": {
                                "_text": "Moon phase"
                              },
                              "style": {
                                "common": {
                                  "font": "caption",
                                  "foreground": {
                                    "color": "white"
                                  },
                                  "opacity": 0.72
                                }
                              }
                            },
                            {
                              "id": "moon_name",
                              "type": "text",
                              "context": {
                                "_text": "@{moonName}"
                              },
                              "style": {
                                "common": {
                                  "font": {
                                    "kind": "headline",
                                    "weight": "bold"
                                  },
                                  "foreground": {
                                    "color": "white"
                                  }
                                },
                                "text": {
                                  "lineLimit": 1,
                                  "minimumScaleFactor": 0.7
                                }
                              }
                            }
                          ]
                        }
                      ]
                    },
                    {
                      "id": "metric_grid",
                      "type": "grid",
                      "style": {
                        "grid": {
                          "columns": [
                            {
                              "kind": "flexible",
                              "minimum": 70,
                              "maximum": 140,
                              "spacing": 6
                            },
                            {
                              "kind": "flexible",
                              "minimum": 70,
                              "maximum": 140,
                              "spacing": 6
                            }
                          ]
                        },
                        "layout": {
                          "maxWidth": "infinity"
                        }
                      },
                      "children": [
                        {
                          "id": "rise_metric",
                          "type": "vstack",
                          "context": {
                            "_spacing": 2,
                            "_alignment": "leading"
                          },
                          "style": {
                            "layout": {
                              "padding": 6
                            },
                            "common": {
                              "background": {
                                "color": "#FFFFFF",
                                "opacity": 0.16
                              },
                              "foreground": {
                                "color": "white"
                              },
                              "cornerRadius": 14
                            }
                          },
                          "children": [
                            {
                              "id": "rise_label",
                              "type": "label",
                              "context": {
                                "_title": "Sunrise",
                                "_icon": "sf://sunrise.fill"
                              },
                              "style": {
                                "common": {
                                  "font": "caption",
                                  "foreground": {
                                    "color": "#FDE68A"
                                  }
                                }
                              }
                            },
                            {
                              "id": "rise_value",
                              "type": "text",
                              "context": {
                                "_text": "{{ suffix(${sunrise}, 5) }}"
                              },
                              "style": {
                                "common": {
                                  "font": {
                                    "kind": "headline",
                                    "weight": "bold"
                                  },
                                  "foreground": {
                                    "color": "white"
                                  }
                                },
                                "text": {
                                  "lineLimit": 1,
                                  "minimumScaleFactor": 0.75
                                }
                              }
                            }
                          ]
                        },
                        {
                          "id": "set_metric",
                          "type": "vstack",
                          "context": {
                            "_spacing": 2,
                            "_alignment": "leading"
                          },
                          "style": {
                            "layout": {
                              "padding": 6
                            },
                            "common": {
                              "background": {
                                "color": "#FFFFFF",
                                "opacity": 0.16
                              },
                              "foreground": {
                                "color": "white"
                              },
                              "cornerRadius": 14
                            }
                          },
                          "children": [
                            {
                              "id": "set_label",
                              "type": "label",
                              "context": {
                                "_title": "Sunset",
                                "_icon": "sf://sunset.fill"
                              },
                              "style": {
                                "common": {
                                  "font": "caption",
                                  "foreground": {
                                    "color": "#FDE68A"
                                  }
                                }
                              }
                            },
                            {
                              "id": "set_value",
                              "type": "text",
                              "context": {
                                "_text": "{{ suffix(${sunset}, 5) }}"
                              },
                              "style": {
                                "common": {
                                  "font": {
                                    "kind": "headline",
                                    "weight": "bold"
                                  },
                                  "foreground": {
                                    "color": "white"
                                  }
                                },
                                "text": {
                                  "lineLimit": 1,
                                  "minimumScaleFactor": 0.75
                                }
                              }
                            }
                          ]
                        },
                        {
                          "id": "day_metric",
                          "type": "vstack",
                          "context": {
                            "_spacing": 2,
                            "_alignment": "leading"
                          },
                          "style": {
                            "layout": {
                              "padding": 6
                            },
                            "common": {
                              "background": {
                                "color": "#FFFFFF",
                                "opacity": 0.16
                              },
                              "foreground": {
                                "color": "white"
                              },
                              "cornerRadius": 14
                            }
                          },
                          "children": [
                            {
                              "id": "day_label",
                              "type": "label",
                              "context": {
                                "_title": "Daylight",
                                "_icon": "sf://sun.max.fill"
                              },
                              "style": {
                                "common": {
                                  "font": "caption",
                                  "foreground": {
                                    "color": "#FDE68A"
                                  }
                                }
                              }
                            },
                            {
                              "id": "day_value",
                              "type": "text",
                              "context": {
                                "_text": "@{daylightText}"
                              },
                              "style": {
                                "common": {
                                  "font": {
                                    "kind": "headline",
                                    "weight": "bold"
                                  },
                                  "foreground": {
                                    "color": "white"
                                  }
                                },
                                "text": {
                                  "lineLimit": 1,
                                  "minimumScaleFactor": 0.65
                                }
                              }
                            }
                          ]
                        },
                        {
                          "id": "phase_metric",
                          "type": "vstack",
                          "context": {
                            "_spacing": 2,
                            "_alignment": "leading"
                          },
                          "style": {
                            "layout": {
                              "padding": 6
                            },
                            "common": {
                              "background": {
                                "color": "#FFFFFF",
                                "opacity": 0.16
                              },
                              "foreground": {
                                "color": "white"
                              },
                              "cornerRadius": 14
                            }
                          },
                          "children": [
                            {
                              "id": "phase_label",
                              "type": "label",
                              "context": {
                                "_title": "Phase",
                                "_icon": "sf://circle.lefthalf.filled"
                              },
                              "style": {
                                "common": {
                                  "font": "caption",
                                  "foreground": {
                                    "color": "#FDE68A"
                                  }
                                }
                              }
                            },
                            {
                              "id": "phase_value",
                              "type": "text",
                              "context": {
                                "_text": "{{ cast(${moonPhase} * 100, 'int') + '%' }}"
                              },
                              "style": {
                                "common": {
                                  "font": {
                                    "kind": "headline",
                                    "weight": "bold"
                                  },
                                  "foreground": {
                                    "color": "white"
                                  }
                                },
                                "text": {
                                  "lineLimit": 1,
                                  "minimumScaleFactor": 0.75
                                }
                              }
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              }
            }
          }
        }
      ]
    }
  ]
}