"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const shared = require("../popup/shared.js");
const index = require("../popup/index.js");
const index$3 = require("../button/index.js");
const index$1 = require("../action-bar/index.js");
const index$2 = require("../action-bar-button/index.js");
const basic = require("../utils/basic.js");
const constant = require("../utils/constant.js");
const create = require("../utils/create.js");
const format = require("../utils/format.js");
const props = require("../utils/props.js");
const interceptor = require("../utils/interceptor.js");
const [name, bem, t] = create.createNamespace("dialog");
const dialogProps = basic.extend({}, shared.popupSharedProps, {
  title: String,
  theme: String,
  width: props.numericProp,
  message: [String, Function],
  callback: Function,
  allowHtml: Boolean,
  className: props.unknownProp,
  transition: props.makeStringProp("van-dialog-bounce"),
  messageAlign: String,
  closeOnPopstate: props.truthProp,
  showCancelButton: Boolean,
  cancelButtonText: String,
  cancelButtonColor: String,
  cancelButtonDisabled: Boolean,
  confirmButtonText: String,
  confirmButtonColor: String,
  confirmButtonDisabled: Boolean,
  showConfirmButton: props.truthProp,
  closeOnClickOverlay: Boolean,
  keyboardEnabled: props.truthProp,
  destroyOnClose: Boolean
});
const popupInheritKeys = [...shared.popupSharedPropKeys, "transition", "closeOnPopstate", "destroyOnClose"];
var stdin_default = Vue.defineComponent({
  name,
  props: dialogProps,
  emits: ["confirm", "cancel", "keydown", "update:show"],
  setup(props2, {
    emit,
    slots
  }) {
    const root = Vue.ref();
    const loading = Vue.reactive({
      confirm: false,
      cancel: false
    });
    const updateShow = (value) => emit("update:show", value);
    const close = (action) => {
      var _a;
      updateShow(false);
      (_a = props2.callback) == null ? void 0 : _a.call(props2, action);
    };
    const getActionHandler = (action) => () => {
      if (!props2.show) {
        return;
      }
      emit(action);
      if (props2.beforeClose) {
        loading[action] = true;
        interceptor.callInterceptor(props2.beforeClose, {
          args: [action],
          done() {
            close(action);
            loading[action] = false;
          },
          canceled() {
            loading[action] = false;
          }
        });
      } else {
        close(action);
      }
    };
    const onCancel = getActionHandler("cancel");
    const onConfirm = getActionHandler("confirm");
    const onKeydown = Vue.withKeys((event) => {
      var _a, _b;
      if (!props2.keyboardEnabled) {
        return;
      }
      if (event.target !== ((_b = (_a = root.value) == null ? void 0 : _a.popupRef) == null ? void 0 : _b.value)) {
        return;
      }
      const onEventType = {
        Enter: props2.showConfirmButton ? onConfirm : basic.noop,
        Escape: props2.showCancelButton ? onCancel : basic.noop
      };
      onEventType[event.key]();
      emit("keydown", event);
    }, ["enter", "esc"]);
    const renderTitle = () => {
      const title = slots.title ? slots.title() : props2.title;
      if (title) {
        return Vue.createVNode("div", {
          "class": bem("header", {
            isolated: !props2.message && !slots.default
          })
        }, [title]);
      }
    };
    const renderMessage = (hasTitle) => {
      const {
        message,
        allowHtml,
        messageAlign
      } = props2;
      const classNames = bem("message", {
        "has-title": hasTitle,
        [messageAlign]: messageAlign
      });
      const content = basic.isFunction(message) ? message() : message;
      if (allowHtml && typeof content === "string") {
        return Vue.createVNode("div", {
          "class": classNames,
          "innerHTML": content
        }, null);
      }
      return Vue.createVNode("div", {
        "class": classNames
      }, [content]);
    };
    const renderContent = () => {
      if (slots.default) {
        return Vue.createVNode("div", {
          "class": bem("content")
        }, [slots.default()]);
      }
      const {
        title,
        message,
        allowHtml
      } = props2;
      if (message) {
        const hasTitle = !!(title || slots.title);
        return Vue.createVNode("div", {
          "key": allowHtml ? 1 : 0,
          "class": bem("content", {
            isolated: !hasTitle
          })
        }, [renderMessage(hasTitle)]);
      }
    };
    const renderButtons = () => Vue.createVNode("div", {
      "class": [constant.BORDER_TOP, bem("footer")]
    }, [props2.showCancelButton && Vue.createVNode(index$3.Button, {
      "size": "large",
      "text": props2.cancelButtonText || t("cancel"),
      "class": bem("cancel"),
      "style": {
        color: props2.cancelButtonColor
      },
      "loading": loading.cancel,
      "disabled": props2.cancelButtonDisabled,
      "onClick": onCancel
    }, null), props2.showConfirmButton && Vue.createVNode(index$3.Button, {
      "size": "large",
      "text": props2.confirmButtonText || t("confirm"),
      "class": [bem("confirm"), {
        [constant.BORDER_LEFT]: props2.showCancelButton
      }],
      "style": {
        color: props2.confirmButtonColor
      },
      "loading": loading.confirm,
      "disabled": props2.confirmButtonDisabled,
      "onClick": onConfirm
    }, null)]);
    const renderRoundButtons = () => Vue.createVNode(index$1.ActionBar, {
      "class": bem("footer")
    }, {
      default: () => [props2.showCancelButton && Vue.createVNode(index$2.ActionBarButton, {
        "type": "warning",
        "text": props2.cancelButtonText || t("cancel"),
        "class": bem("cancel"),
        "color": props2.cancelButtonColor,
        "loading": loading.cancel,
        "disabled": props2.cancelButtonDisabled,
        "onClick": onCancel
      }, null), props2.showConfirmButton && Vue.createVNode(index$2.ActionBarButton, {
        "type": "danger",
        "text": props2.confirmButtonText || t("confirm"),
        "class": bem("confirm"),
        "color": props2.confirmButtonColor,
        "loading": loading.confirm,
        "disabled": props2.confirmButtonDisabled,
        "onClick": onConfirm
      }, null)]
    });
    const renderFooter = () => {
      if (slots.footer) {
        return slots.footer();
      }
      return props2.theme === "round-button" ? renderRoundButtons() : renderButtons();
    };
    return () => {
      const {
        width,
        title,
        theme,
        message,
        className
      } = props2;
      return Vue.createVNode(index.Popup, Vue.mergeProps({
        "ref": root,
        "role": "dialog",
        "class": [bem([theme]), className],
        "style": {
          width: format.addUnit(width)
        },
        "tabindex": 0,
        "aria-labelledby": title || message,
        "onKeydown": onKeydown,
        "onUpdate:show": updateShow
      }, basic.pick(props2, popupInheritKeys)), {
        default: () => [renderTitle(), renderContent(), renderFooter()]
      });
    };
  }
});
exports.default = stdin_default;
exports.dialogProps = dialogProps;
