import { defineComponent as b, watch as U, createElementBlock as i, openBlock as s, createCommentVNode as c, normalizeStyle as u, unref as r, toDisplayString as n, createTextVNode as z, ref as f, onMounted as H, onBeforeUnmount as R } from "vue";
import W from "axios";
import { autoInstallFont as B, jsonToCssStyle as C } from "../../commons/utils/json2css.mjs";
const D = {
  key: 0,
  class: "weather-temperature__suffix"
}, A = b({ name: "zv-commons-weather" }), N = /* @__PURE__ */ b({
  ...A,
  props: {
    basicOption: {},
    useEvents: {}
  },
  emits: [
    "on-success",
    "on-error"
  ],
  setup(g, { emit: O }) {
    const o = g, m = O, S = () => {
      const t = f(), p = f({
        city: "",
        date: "",
        week: "",
        dayweather: "",
        nightweather: "",
        daytemp: "",
        nighttemp: "",
        daywind: "",
        nightwind: "",
        daypower: "",
        nightpower: "",
        daytemp_float: "",
        nighttemp_float: ""
      }), h = f(null), w = (e) => {
        W.get(`https://restapi.amap.com/v3/weather/weatherInfo?city=${e.city}&key=${e.key}&extensions=all`).then((a) => {
          a.status === 200 && a.data.status === "1" && a.data.forecasts.length > 0 ? (m("on-success", a), window.SHJParseEvent(o.useEvents, "on-success", a), p.value = a.data.forecasts[0].casts[0], p.value.city = a.data.forecasts[0].city) : (m("on-error", a), window.SHJParseEvent(o.useEvents, "on-error", a));
        }).catch(() => {
          m("on-error", null), window.SHJParseEvent(o.useEvents, "on-error", null);
        });
      }, E = (e) => (e.fontFamily && B(e.fontFamily), {
        ...C(e),
        lineHeight: `${e.lineHeight}px`,
        transform: `translate(${e.transform.x}px, ${e.transform.y}px)`
      }), I = (e, a) => {
        if (a.find((y) => y.name === p.value.dayweather)) {
          const y = a.find((T) => T.name === p.value.dayweather).url || "";
          return {
            mask: `url(${y}) 0px 0px / 100% 100%`,
            width: `${e.size}px`,
            height: `${e.size}px`,
            background: `url(${y}) 0px 0px / 100% 100%, ${e.color}`,
            backgroundBlendMode: "overlay",
            marginRight: `${e.margin.right}px`
          };
        }
        return {};
      }, v = (e) => {
        h.value && clearInterval(h.value), e.autoUpdate !== 0 ? h.value = setInterval(() => w(e), e.autoUpdate * 1e3) : (clearInterval(h.value), w(e));
      };
      return H(() => {
        w(o.basicOption), v(o.basicOption);
      }), R(() => {
        h.value && clearInterval(h.value);
      }), {
        initWeatherMessage: w,
        rendererDomStyle: E,
        handleAutoUpdate: v,
        rendererWeatherTypeStyle: I,
        weatherRef: t,
        weather: p
      };
    }, { handleAutoUpdate: k, rendererDomStyle: d, rendererWeatherTypeStyle: $, weatherRef: x, weather: l } = S();
    return U(() => o.basicOption, () => k(o.basicOption), { deep: !0 }), (t, p) => (s(), i("div", {
      ref_key: "weatherRef",
      ref: x,
      class: "zerov-widgets"
    }, [
      t.basicOption.icon.show ? (s(), i("p", {
        key: 0,
        class: "weather-icon",
        style: u(r($)(t.basicOption.icon, t.basicOption.icons))
      }, null, 4)) : c("", !0),
      t.basicOption.weather.city.show ? (s(), i("p", {
        key: 1,
        class: "weather-city",
        style: u(r(d)(t.basicOption.weather.city.textStyle))
      }, n(r(l).city), 5)) : c("", !0),
      t.basicOption.weather.type.show ? (s(), i("p", {
        key: 2,
        class: "weather-type",
        style: u(r(d)(t.basicOption.weather.type.textStyle))
      }, n(r(l).dayweather), 5)) : c("", !0),
      t.basicOption.weather.wind.show ? (s(), i("p", {
        key: 3,
        class: "weather-wind",
        style: u(r(d)(t.basicOption.weather.wind.textStyle))
      }, n(r(l).daywind) + "风" + n(r(l).daypower) + "级 ", 5)) : c("", !0),
      t.basicOption.weather.temperature.show ? (s(), i("p", {
        key: 4,
        class: "weather-temperature",
        style: u(r(d)(t.basicOption.weather.temperature.textStyle))
      }, [
        z(n(r(l).nighttemp) + " " + n(t.basicOption.weather.temperature.connectors) + " " + n(r(l).daytemp) + " ", 1),
        t.basicOption.weather.temperature.suffix.show ? (s(), i("span", D, n(t.basicOption.weather.temperature.suffix.suffix), 1)) : c("", !0)
      ], 4)) : c("", !0)
    ], 512));
  }
});
export {
  N as default
};
