/**
 * ldesign v0.2.14
 * (c) 2022/11/2 16:16 built by swimly
 * @license MIT
 */

import isNumber from 'lodash/isNumber';
import throttle from 'lodash/throttle';
import { InfoCircleFilledIcon, CheckCircleFilledIcon, ErrorCircleFilledIcon, CloseIcon } from '@ldesign/icons-vue';
import { Button } from '../button/index.js';
import { renderContent, renderLNodeJSX } from '../../utils/render-node.js';
import mixins from '../../utils/mixins.js';
import getConfigReceiverMixins, { getGlobalIconMixins } from '../config-provider/config-receiver.js';
import TransferDom from '../../utils/transfer-dom.js';
import { emitEvent } from '../../utils/event.js';
import { addClass, removeClass } from '../../utils/dom.js';
import { updateElement } from '../../hooks/use-destroy-onclose.js';
import props from './props.js';
import ActionMixin from './mixins/action.js';
import stack from './stack.js';
import '../../utils/with-install.js';
import 'vue';
import 'lodash';
import '../../config.js';
import '../button/button.js';
import '../loading/index.js';
import '../loading/loading.js';
import '../loading/props.js';
import '../loading/icon/gradient.js';
import '../../chunks/defineProperty-838e4192.js';
import '../../chunks/set-style.js';
import '../../chunks/helper2.js';
import 'raf';
import 'lodash/isString';
import '../../chunks/index.js';
import '../../chunks/log.js';
import '../../chunks/_commonjsHelpers-2e6815bd.js';
import '../../utils/easing.js';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import '../../chunks/zh_CN.js';
import '../config-provider/type.js';
import '../loading/style/css.js';
import '../loading/type.js';
import '../loading/plugin.js';
import '../../utils/ripple.js';
import '../button/props.js';
import '../button/style/css.js';
import '../button/type.js';
import '../../utils/helper.js';
import 'lodash/camelCase';
import '../../chunks/helper.js';
import '../../chunks/_commonjsHelpers.js';
import 'lodash/isObject';

function getCSSValue(v) {
  return isNaN(Number(v)) ? v : `${Number(v)}px`;
}
let mousePosition;
const getClickPosition = e => {
  mousePosition = {
    x: e.clientX,
    y: e.clientY
  };
  setTimeout(() => {
    mousePosition = null;
  }, 100);
};
if (typeof window !== "undefined" && window.document && window.document.documentElement) document.documentElement.addEventListener("click", getClickPosition, true);
var _Dialog = mixins(ActionMixin, getConfigReceiverMixins("dialog"), getGlobalIconMixins()).extend({
  name: "LDialog",
  components: {
    TButton: Button
  },
  data() {
    return {
      uid: 0,
      scrollWidth: 0,
      disX: 0,
      disY: 0,
      windowInnerWidth: 0,
      windowInnerHeight: 0,
      dialogW: 0,
      dialogH: 0,
      dLeft: 0,
      dTop: 0
    };
  },
  props: {
    ...props
  },
  computed: {
    isModal() {
      return this.mode === "modal";
    },
    isModeLess() {
      return this.mode === "modeless";
    },
    isNormal() {
      return this.mode === "normal";
    },
    maskClass() {
      return [`${this.componentName}__mask`, !this.showOverlay && `${this.classPrefix}-is-hidden`];
    },
    dialogClass() {
      const dialogClass = [`${this.componentName}`, `${this.componentName}--default`, `${this.componentName}__modal-${this.theme}`, this.isModeLess && this.draggable && `${this.componentName}--draggable`];
      return dialogClass;
    },
    positionClass() {
      if (this.isNormal) return [];
      const dialogClass = [`${this.componentName}__position`, !!this.top && `${this.componentName}--top`, `${this.placement && !this.top ? `${this.componentName}--${this.placement}` : ""}`];
      return dialogClass;
    },
    wrapClass() {
      return [!this.isNormal && `${this.componentName}__wrap`];
    },
    bodyClass() {
      return [`${this.componentName}__body`, {
        [`${this.componentName}__body--icon`]: this.theme !== "default"
      }];
    },
    ctxClass() {
      return [`${this.componentName}__ctx`, {
        [`${this.classPrefix}-dialog__ctx--fixed`]: this.mode === "modal",
        [`${this.classPrefix}-dialog__ctx--absolute`]: this.isModal && this.showInAttachedElement,
        [`${this.componentName}__ctx--modeless`]: this.isModeLess
      }];
    },
    positionStyle() {
      const topStyle = {};
      if (this.top !== void 0) {
        if (isNumber(this.top) && this.top < 0) topStyle.paddingTop = `${this.top}px`;else topStyle.paddingTop = this.top;
      }
      return topStyle;
    },
    dialogStyle() {
      return {
        width: getCSSValue(this.width)
      };
    }
  },
  watch: {
    visible(value) {
      if (value) {
        const {
          scrollWidth
        } = this;
        if (this.isModal && !this.showInAttachedElement) {
          if (scrollWidth > 0 && this.preventScrollThrough) {
            const bodyCssText = `position: relative;width: calc(100% - ${scrollWidth}px);`;
            document.body.style.cssText = bodyCssText;
          }
          this.preventScrollThrough && addClass(document.body, `${this.componentName}--lock`);
          this.$nextTick(() => {
            const target = this.$refs.dialog;
            if (mousePosition && target) {
              target.style.transformOrigin = `${mousePosition.x - target.offsetLeft}px ${mousePosition.y - target.offsetTop}px`;
            }
          });
        }
        document.activeElement.blur();
      } else {
        document.body.style.cssText = "";
        removeClass(document.body, `${this.componentName}--lock`);
      }
      this.storeUid(value);
      this.addKeyboardEvent(value);
      if (this.isModeLess && this.draggable) this.initDragEvent(value);
      if (value && !this.destroyOnClose && requestAnimationFrame) {
        requestAnimationFrame(() => {
          updateElement(this);
        });
      }
    }
  },
  mounted() {
    this.scrollWidth = window.innerWidth - document.body.offsetWidth;
    if (this.draggable) window.addEventListener("resize", throttle(this.resizeAdjustPosition, 1e3));
    if (this.visible && this.isModal && this.preventScrollThrough) addClass(document.body, `${this.componentName}--lock`);
    this.uid = this._uid;
  },
  beforeDestroy() {
    this.addKeyboardEvent(false);
  },
  directives: {
    TransferDom
  },
  methods: {
    storeUid(flag) {
      if (flag) stack.push(this.uid);else stack.pop();
    },
    addKeyboardEvent(status) {
      if (status) {
        document.addEventListener("keydown", this.keyboardEvent);
        this.confirmOnEnter && document.addEventListener("keydown", this.keyboardEnterEvent);
      } else {
        document.removeEventListener("keydown", this.keyboardEvent);
        this.confirmOnEnter && document.removeEventListener("keydown", this.keyboardEnterEvent);
      }
    },
    keyboardEvent(e) {
      if (e.code === "Escape" && stack.top === this.uid) {
        emitEvent(this, "esc-keydown", {
          e
        });
        if (this.closeOnEscKeydown ?? this.global.closeOnEscKeydown) this.emitCloseEvent({
          e,
          trigger: "esc"
        });
      }
    },
    keyboardEnterEvent(e) {
      const {
        code
      } = e;
      if ((code === "Enter" || code === "NumpadEnter") && stack.top === this.uid) emitEvent(this, "confirm", {
        e
      });
    },
    overlayAction(e) {
      if (this.showOverlay && (this.closeOnOverlayClick ?? this.global.closeOnOverlayClick)) {
        if (e.target === this.$refs.dialogPosition) {
          emitEvent(this, "overlay-click", {
            e
          });
          this.emitCloseEvent({
            e,
            trigger: "overlay"
          });
        }
      }
    },
    closeBtnAction(e) {
      emitEvent(this, "close-btn-click", {
        e
      });
      this.emitCloseEvent({
        trigger: "close-btn",
        e
      });
    },
    cancelBtnAction(e) {
      emitEvent(this, "cancel", {
        e
      });
      this.emitCloseEvent({
        trigger: "cancel",
        e
      });
    },
    confirmBtnAction(e) {
      emitEvent(this, "confirm", {
        e
      });
    },
    afterEnter() {
      emitEvent(this, "opened");
    },
    afterLeave() {
      if (this.isModeLess && this.draggable) {
        const target = this.$refs.dialog;
        target.style.position = "relative";
        target.style.left = "unset";
        target.style.top = "unset";
      }
      emitEvent(this, "closed");
    },
    emitCloseEvent(context) {
      emitEvent(this, "close", context);
      this.$emit("update:visible", false);
    },
    hasEventOn(name) {
      var _this$_events;
      const eventFuncs = (_this$_events = this["_events"]) === null || _this$_events === void 0 ? void 0 : _this$_events[name];
      return !!(eventFuncs !== null && eventFuncs !== void 0 && eventFuncs.length);
    },
    getIcon() {
      const h = this.$createElement;
      const {
        InfoCircleFilledIcon: InfoCircleFilledIcon$1,
        CheckCircleFilledIcon: CheckCircleFilledIcon$1,
        ErrorCircleFilledIcon: ErrorCircleFilledIcon$1
      } = this.useGlobalIcon({
        InfoCircleFilledIcon: InfoCircleFilledIcon,
        CheckCircleFilledIcon: CheckCircleFilledIcon,
        ErrorCircleFilledIcon: ErrorCircleFilledIcon
      });
      const icon = {
        info: h(InfoCircleFilledIcon$1, {
          "class": `${this.classPrefix}-is-info`
        }),
        warning: h(ErrorCircleFilledIcon$1, {
          "class": `${this.classPrefix}-is-warning`
        }),
        danger: h(ErrorCircleFilledIcon$1, {
          "class": `${this.classPrefix}-is-error`
        }),
        success: h(CheckCircleFilledIcon$1, {
          "class": `${this.classPrefix}-is-success`
        })
      };
      return icon[this.theme];
    },
    mousedownHandler(targetEvent) {
      const target = this.$refs.dialog;
      this.disX = targetEvent.clientX - target.offsetLeft;
      this.disY = targetEvent.clientY - target.offsetTop;
      this.dialogW = target.offsetWidth;
      this.dialogH = target.offsetHeight;
      this.windowInnerWidth = window.innerWidth || document.documentElement.clientWidth;
      this.windowInnerHeight = window.innerHeight || document.documentElement.clientHeight;
      if (this.dialogW > this.windowInnerWidth || this.dialogH > this.windowInnerHeight) return;
      document.addEventListener("mousemove", this.mouseMoverHandler);
      document.addEventListener("mouseup", this.mouseUpHandler);
      document.addEventListener("dragend", this.mouseUpHandler);
    },
    mouseMoverHandler(documentEvent) {
      const target = this.$refs.dialog;
      let left = documentEvent.clientX - this.disX;
      let top = documentEvent.clientY - this.disY;
      if (left < 0) left = 0;
      if (top < 0) top = 0;
      if (this.windowInnerWidth - target.offsetWidth - left < 0) left = this.windowInnerWidth - target.offsetWidth;
      if (this.windowInnerHeight - target.offsetHeight - top < 0) top = this.windowInnerHeight - target.offsetHeight;
      target.style.position = "absolute";
      target.style.left = `${left}px`;
      target.style.top = `${top}px`;
    },
    mouseUpHandler() {
      document.removeEventListener("mousemove", this.mouseMoverHandler);
      document.removeEventListener("mouseup", this.mouseUpHandler);
      document.removeEventListener("dragend", this.mouseUpHandler);
    },
    initDragEvent(status) {
      const target = this.$refs.dialog;
      if (status) target.addEventListener("mousedown", this.mousedownHandler);else target.removeEventListener("mousedown", this.mousedownHandler);
    },
    resizeAdjustPosition() {
      if (this.visible) {
        const target = this.$refs.dialog;
        target.style.left = `${this.dLeft * (window.innerWidth / this.windowInnerWidth)}px`;
        target.style.top = `${this.dTop * (window.innerHeight / this.windowInnerHeight)}px`;
      }
    },
    onStopDown(e) {
      if (this.isModeLess && this.draggable) e.stopPropagation();
    },
    renderDialog() {
      const h = this.$createElement;
      const {
        CloseIcon: CloseIcon$1
      } = this.useGlobalIcon({
        CloseIcon: CloseIcon
      });
      const defaultHeader = h("h5", {
        "class": "title"
      });
      const defaultCloseBtn = h(CloseIcon$1);
      const body = renderContent(this, "default", "body");
      const defaultFooter = h("div", [this.getCancelBtn({
        cancelBtn: this.cancelBtn,
        globalCancel: this.global.cancel,
        className: `${this.componentName}__cancel`
      }), this.getConfirmBtn({
        theme: this.theme,
        confirmBtn: this.confirmBtn,
        globalConfirm: this.global.confirm,
        globalConfirmBtnTheme: this.global.confirmBtnTheme,
        className: `${this.componentName}__confirm`
      })]);
      const footer = this.footer ? h("div", {
        "class": `${this.componentName}__footer`,
        "on": {
          "mousedown": this.onStopDown
        }
      }, [renderLNodeJSX(this, "footer", defaultFooter)]) : null;
      return h("div", {
        "class": this.wrapClass
      }, [h("div", {
        "class": this.positionClass,
        "style": this.positionStyle,
        "on": {
          "click": this.overlayAction
        },
        "ref": "dialogPosition"
      }, [h("div", {
        "key": "dialog",
        "ref": "dialog",
        "class": this.dialogClass,
        "style": this.dialogStyle
      }, [h("div", {
        "class": `${this.componentName}__header`,
        "on": {
          "mousedown": this.onStopDown
        }
      }, [this.getIcon(), renderLNodeJSX(this, "header", defaultHeader)]), this.closeBtn ? h("span", {
        "class": `${this.componentName}__close`,
        "on": {
          "click": this.closeBtnAction
        }
      }, [renderLNodeJSX(this, "closeBtn", defaultCloseBtn)]) : null, h("div", {
        "class": this.bodyClass,
        "on": {
          "mousedown": this.onStopDown
        }
      }, [body]), footer])])]);
    }
  },
  render() {
    const h = arguments[0];
    const maskView = this.isModal && h("div", {
      "key": "mask",
      "class": this.maskClass
    });
    const dialogView = this.renderDialog();
    const view = [maskView, dialogView];
    const ctxStyle = {
      zIndex: this.zIndex
    };
    return h("transition", {
      "attrs": {
        "duration": 300,
        "name": `${this.componentName}-zoom__vue`
      },
      "on": {
        "afterEnter": this.afterEnter,
        "afterLeave": this.afterLeave
      }
    }, [(!this.destroyOnClose || this.visible) && h("div", {
      "directives": [{
        name: "show",
        value: this.visible
      }, {
        name: "transfer-dom",
        value: this.attach
      }],
      "class": this.ctxClass,
      "style": ctxStyle
    }, [view])]);
  }
});

export { _Dialog as default };
//# sourceMappingURL=dialog.js.map
