var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __async = (__this, __arguments, generator) => {
  return new Promise((resolve, reject) => {
    var fulfilled = (value) => {
      try {
        step(generator.next(value));
      } catch (e) {
        reject(e);
      }
    };
    var rejected = (value) => {
      try {
        step(generator.throw(value));
      } catch (e) {
        reject(e);
      }
    };
    var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
    step((generator = generator.apply(__this, __arguments)).next());
  });
};
import { createVNode as _createVNode, Fragment as _Fragment } from "vue";
import { CloseOutlined, CopyOutlined, NotificationOutlined, SettingOutlined } from "@ant-design/icons-vue";
import { merge } from "@antdv/pro-provider";
import { anyType, boolType, funcType, isBrowser, objectType, omit, oneOfType, stringType, useMountMergeState, useState, useUrlSearchParams } from "@antdv/pro-utils";
import { Alert, Button, Divider, Drawer, List, message, Switch } from "ant-design-vue";
import { computed, defineComponent, onMounted, onUnmounted, ref, watch, watchEffect } from "vue";
import { defaultSettings } from "../../defaultSettings.mjs";
import { getLanguage, gLocaleObject } from "../../locales/index.mjs";
import { genStringToTheme } from "../../utils/utils.mjs";
import { BlockCheckbox } from "./BlockCheckbox.mjs";
import { GroupIcon } from "./icon/group.mjs";
import { SubIcon } from "./icon/sub.mjs";
import { LayoutSetting, renderLayoutSettingItem } from "./LayoutChange.mjs";
import { RegionalSetting } from "./RegionalChange.mjs";
import { useStyle } from "./style/index.mjs";
import { ThemeColor } from "./ThemeColor.mjs";
const Body = defineComponent({
  name: "Body",
  props: {
    title: stringType(),
    prefixCls: stringType(),
    hashId: stringType()
  },
  setup(props, {
    slots
  }) {
    return () => {
      var _a;
      return _createVNode("div", {
        "style": {
          marginBlockEnd: `12px`
        }
      }, [_createVNode("h3", {
        "class": `${props.prefixCls}-body-title ${props.hashId}`.trim()
      }, [props.title]), (_a = slots.default) == null ? void 0 : _a.call(slots)]);
    };
  }
});
const settingDrawerProps = {
  defaultSettings: objectType(),
  settings: objectType(),
  collapse: boolType(),
  onCollapseChange: funcType(),
  getContainer: anyType(),
  hideHintAlert: boolType(),
  hideCopyButton: boolType(),
  enableDarkTheme: boolType(),
  prefixCls: stringType(),
  colorList: oneOfType([Boolean, Array]).def(void 0),
  onSettingChange: funcType(),
  pathname: stringType(),
  disableUrlParams: boolType().def(true),
  themeOnly: boolType()
};
function getDifferentSetting(state) {
  const stateObj = {};
  Object.keys(state).forEach((key) => {
    if (state[key] !== defaultSettings[key] && key !== "collapse") {
      stateObj[key] = state[key];
    } else {
      stateObj[key] = void 0;
    }
    if (key.includes("Render")) {
      stateObj[key] = state[key] === false ? false : void 0;
    }
  });
  stateObj.menu = void 0;
  return stateObj;
}
function getFormatMessage() {
  const formatMessage = ({
    id
  }) => {
    const locales = gLocaleObject();
    return locales[id];
  };
  return formatMessage;
}
function initState(urlParams, settings, onSettingChange) {
  if (!isBrowser()) return;
  const replaceSetting = {};
  Object.keys(urlParams).forEach((key) => {
    if (defaultSettings[key] || defaultSettings[key] === void 0) {
      if (key === "colorPrimary") {
        replaceSetting[key] = genStringToTheme(urlParams[key]);
        return;
      }
      replaceSetting[key] = urlParams[key];
    }
  });
  const newSettings = merge({}, settings, replaceSetting);
  delete newSettings.menu;
  delete newSettings.title;
  delete newSettings.iconfontUrl;
  onSettingChange == null ? void 0 : onSettingChange(newSettings);
}
function getParamsFromUrl(urlParams, settings) {
  if (!isBrowser()) return defaultSettings;
  return __spreadValues(__spreadValues(__spreadValues({}, defaultSettings), settings || {}), urlParams);
}
function genCopySettingJson(settingState) {
  return JSON.stringify(omit(__spreadProps(__spreadValues({}, settingState), {
    colorPrimary: settingState.colorPrimary
  }), ["colorWeak"]), null, 2);
}
const SettingDrawer = defineComponent({
  name: "SettingDrawer",
  props: settingDrawerProps,
  setup(props) {
    var _a;
    const firstRender = ref(true);
    const open = ref(props.collapse !== void 0 ? props.collapse : false);
    const [language, setLanguage] = useState(getLanguage());
    watch(() => props.collapse, () => {
      open.value = !!props.collapse;
    });
    const [urlParams, setUrlParams] = useUrlSearchParams({}, {
      disabled: (_a = props.disableUrlParams) != null ? _a : true
    });
    const [settingState, setSettingState] = useMountMergeState(() => getParamsFromUrl(urlParams, props.settings || props.defaultSettings), {
      value: computed(() => props.settings),
      onChange: props.onSettingChange
    });
    const onLanguageChange = () => {
      if (language.value !== getLanguage()) {
        setLanguage(getLanguage());
      }
    };
    onMounted(() => {
      if (!isBrowser()) return;
      initState(getParamsFromUrl(urlParams, props.settings), settingState.value, setSettingState);
      window.document.addEventListener("languagechange", onLanguageChange, {
        passive: true
      });
    });
    onUnmounted(() => {
      window.document.removeEventListener("languagechange", onLanguageChange);
    });
    watchEffect(() => {
      if (!isBrowser()) return;
      if (props.disableUrlParams) return;
      if (firstRender.value) {
        firstRender.value = false;
        return;
      }
      const urlSearchParams = new URLSearchParams(window.location.search);
      const params = Object.fromEntries(urlSearchParams.entries());
      const diffParams = getDifferentSetting(__spreadValues(__spreadValues({}, params), settingState.value));
      delete diffParams.logo;
      delete diffParams.menu;
      delete diffParams.title;
      delete diffParams.iconfontUrl;
      delete diffParams.pwa;
      setUrlParams(diffParams);
    });
    const baseClassName = computed(() => `${props.prefixCls || "ant-pro"}-setting-drawer`);
    const {
      wrapSSR,
      hashId
    } = useStyle(baseClassName);
    return () => {
      const {
        hideHintAlert,
        hideCopyButton,
        colorList = [{
          key: "techBlue",
          color: "#1677FF"
        }, {
          key: "daybreak",
          color: "#1890ff"
        }, {
          key: "dust",
          color: "#F5222D"
        }, {
          key: "volcano",
          color: "#FA541C"
        }, {
          key: "sunset",
          color: "#FAAD14"
        }, {
          key: "cyan",
          color: "#13C2C2"
        }, {
          key: "green",
          color: "#52C41A"
        }, {
          key: "geekblue",
          color: "#2F54EB"
        }, {
          key: "purple",
          color: "#722ED1"
        }],
        getContainer,
        enableDarkTheme,
        themeOnly
      } = props;
      const {
        navTheme,
        colorPrimary,
        siderMenuType,
        layout,
        colorWeak
      } = settingState.value || {};
      const changeSetting = (key, value) => {
        const nextState = {};
        nextState[key] = value;
        if (key === "layout") {
          nextState.contentWidth = value === "top" ? "Fixed" : "Fluid";
        }
        if (key === "layout" && value !== "mix") {
          nextState.splitMenus = false;
        }
        if (key === "layout" && value === "mix") {
          nextState.navTheme = "light";
        }
        if (key === "colorWeak" && value === true) {
          const dom = document.querySelector("body");
          if (dom) {
            dom.dataset.prosettingdrawer = dom.style.filter;
            dom.style.filter = "invert(80%)";
          }
        }
        if (key === "colorWeak" && value === false) {
          const dom = document.querySelector("body");
          if (dom) {
            dom.style.filter = dom.dataset.prosettingdrawer || "none";
            delete dom.dataset.prosettingdrawer;
          }
        }
        delete nextState.menu;
        delete nextState.title;
        delete nextState.iconfontUrl;
        delete nextState.logo;
        delete nextState.pwa;
        setSettingState(__spreadValues(__spreadValues({}, settingState.value), nextState));
      };
      const formatMessage = getFormatMessage();
      return wrapSSR(_createVNode(_Fragment, null, [_createVNode("div", {
        "class": `${baseClassName.value}-handle ${hashId.value}`.trim(),
        "onClick": () => {
          var _a2;
          open.value = !open.value;
          (_a2 = props.onCollapseChange) == null ? void 0 : _a2.call(props, open.value);
        },
        "style": {
          width: `48px`,
          height: `48px`
        }
      }, [open.value ? _createVNode(CloseOutlined, {
        "style": {
          color: "#fff",
          fontSize: `20px`
        }
      }, null) : _createVNode(SettingOutlined, {
        "style": {
          color: "#fff",
          fontSize: `20px`
        }
      }, null)]), _createVNode(Drawer, {
        "open": open.value,
        "width": 300,
        "onClose": () => {
          var _a2;
          open.value = false;
          (_a2 = props.onCollapseChange) == null ? void 0 : _a2.call(props, false);
        },
        "closable": false,
        "placement": "right",
        "getContainer": getContainer,
        "style": {
          zIndex: 999
        }
      }, {
        default: () => [_createVNode("div", {
          "class": `${baseClassName.value}-drawer-content ${hashId.value}`.trim()
        }, [_createVNode(Body, {
          "title": formatMessage({
            id: "app.setting.pagestyle",
            defaultMessage: "Page style setting"
          }),
          "hashId": hashId.value,
          "prefixCls": baseClassName.value
        }, {
          default: () => [_createVNode(BlockCheckbox, {
            "hashId": hashId.value,
            "prefixCls": baseClassName.value,
            "list": [{
              key: "light",
              title: formatMessage({
                id: "app.setting.pagestyle.light",
                defaultMessage: "\u4EAE\u8272\u83DC\u5355\u98CE\u683C"
              })
            }, {
              key: "realDark",
              title: formatMessage({
                id: "app.setting.pagestyle.realdark",
                defaultMessage: "\u6697\u8272\u83DC\u5355\u98CE\u683C"
              })
            }].filter((item) => {
              if (item.key === "dark" && settingState.value.layout === "mix") return false;
              if (item.key === "realDark" && !enableDarkTheme) return false;
              return true;
            }),
            "value": navTheme,
            "configType": "theme",
            "key": "navTheme",
            "onChange": (value) => changeSetting("navTheme", value)
          }, null)]
        }), colorList !== false && _createVNode(Body, {
          "hashId": hashId.value,
          "title": formatMessage({
            id: "app.setting.themecolor",
            defaultMessage: "Theme color"
          }),
          "prefixCls": baseClassName.value
        }, {
          default: () => [_createVNode(ThemeColor, {
            "hashId": hashId.value,
            "prefixCls": baseClassName.value,
            "colorList": colorList,
            "value": genStringToTheme(colorPrimary),
            "formatMessage": formatMessage,
            "onChange": (color) => changeSetting("colorPrimary", color)
          }, null)]
        }), !themeOnly && _createVNode(_Fragment, null, [_createVNode(Divider, null, null), _createVNode(Body, {
          "hashId": hashId.value,
          "prefixCls": baseClassName.value,
          "title": formatMessage({
            id: "app.setting.navigationmode"
          })
        }, {
          default: () => [_createVNode(BlockCheckbox, {
            "prefixCls": baseClassName.value,
            "value": layout,
            "key": "layout",
            "hashId": hashId.value,
            "configType": "layout",
            "list": [{
              key: "side",
              title: formatMessage({
                id: "app.setting.sidemenu"
              })
            }, {
              key: "top",
              title: formatMessage({
                id: "app.setting.topmenu"
              })
            }, {
              key: "mix",
              title: formatMessage({
                id: "app.setting.mixmenu"
              })
            }],
            "onChange": (value) => changeSetting("layout", value)
          }, null)]
        }), settingState.value.layout === "side" || settingState.value.layout === "mix" ? _createVNode(Body, {
          "hashId": hashId.value,
          "prefixCls": baseClassName.value,
          "title": formatMessage({
            id: "app.setting.sidermenutype"
          })
        }, {
          default: () => [_createVNode(BlockCheckbox, {
            "prefixCls": baseClassName.value,
            "value": siderMenuType,
            "key": "siderMenuType",
            "hashId": hashId.value,
            "configType": "siderMenuType",
            "list": [{
              key: "sub",
              icon: _createVNode(SubIcon, null, null),
              title: formatMessage({
                id: "app.setting.sidermenutype-sub"
              })
            }, {
              key: "group",
              icon: _createVNode(GroupIcon, null, null),
              title: formatMessage({
                id: "app.setting.sidermenutype-group"
              })
            }],
            "onChange": (value) => changeSetting("siderMenuType", value)
          }, null)]
        }) : null, _createVNode(LayoutSetting, {
          "prefixCls": baseClassName.value,
          "hashId": hashId.value,
          "settings": settingState.value,
          "changeSetting": changeSetting
        }, null), _createVNode(Divider, null, null), _createVNode(Body, {
          "hashId": hashId.value,
          "prefixCls": baseClassName.value,
          "title": formatMessage({
            id: "app.setting.regionalsettings"
          })
        }, {
          default: () => [_createVNode(RegionalSetting, {
            "hashId": hashId.value,
            "prefixCls": baseClassName.value,
            "settings": settingState.value,
            "changeSetting": changeSetting
          }, null)]
        }), _createVNode(Divider, null, null), _createVNode(Body, {
          "hashId": hashId.value,
          "prefixCls": baseClassName.value,
          "title": formatMessage({
            id: "app.setting.othersettings"
          })
        }, {
          default: () => [_createVNode(List, {
            "class": `${baseClassName.value}-list ${hashId.value}`.trim(),
            "split": false,
            "size": "small",
            "renderItem": renderLayoutSettingItem,
            "dataSource": [{
              title: formatMessage({
                id: "app.setting.weakmode"
              }),
              action: _createVNode(Switch, {
                "size": "small",
                "class": "color-weak",
                "checked": !!colorWeak,
                "onChange": (checked) => {
                  changeSetting("colorWeak", checked);
                }
              }, null)
            }]
          }, null)]
        }), hideHintAlert && hideCopyButton ? null : _createVNode(Divider, null, null), hideHintAlert ? null : _createVNode(Alert, {
          "type": "warning",
          "message": formatMessage({
            id: "app.setting.production.hint"
          }),
          "icon": _createVNode(NotificationOutlined, null, null),
          "showIcon": true,
          "style": {
            marginBlockEnd: `16px`
          }
        }, null), hideCopyButton ? null : _createVNode(Button, {
          "block": true,
          "icon": _createVNode(CopyOutlined, null, null),
          "style": {
            marginBlockEnd: `24px`
          },
          "onClick": () => __async(this, null, function* () {
            try {
              yield navigator.clipboard.writeText(genCopySettingJson(settingState.value));
              message.success(formatMessage({
                id: "app.setting.copyinfo"
              }));
            } catch (error) {
            }
          })
        }, {
          default: () => [formatMessage({
            id: "app.setting.copy"
          })]
        })])])]
      })]));
    };
  }
});
export {
  SettingDrawer,
  getFormatMessage,
  settingDrawerProps
};
