/** @jsx createElement */

'use strict';

import { createElement, findDOMNode, render } from 'rax';
import dom from 'nuke-dom';
import Dialog from './dialog';

const noop = () => {};

let dialogInstance = null;


let container = null;

export function show(options = {}, onConfirm, onCancel, onShow, onFail) {
  onConfirm = onConfirm || noop;
  onShow = onShow || noop;
  onFail = onFail || noop;
  onCancel = onCancel || noop;

  const { duration, maskClosable, contentStyle, onMaskPress, type, locale, ...others } = options;

  const attrs = {
    duration,
    maskClosable,
    contentStyle,
    onShow,
    onFail,
    onMaskPress,
    type,
  };
  if (locale) {
    attrs.locale = locale;
  }
  const dialog = <Dialog {...others} {...attrs} />;
  if (options.container) {
    container = findDOMNode(options.container);
  }
  if (!container) {
    container = dom.createEle('div', { t: 'id', abc: '_nuke_wrap' });
    document.body.appendChild(container);
  }
  render(dialog, container, function () {
    dialogInstance = this;
    dialogInstance.wrappedInstance.show();
  });
}

export function hide() {
  dialogInstance.wrappedInstance.hide();
}

export function confirm(options = {}, callback) {
  show({
    maskClosable: false,

    ...options,

    showCancelButton: true,
    showConfirmButton: true,
  });
}
export function alert(options = {}, callback) {
  show({
    maskClosable: false,
    ...options,
    showConfirmButton: true,
  });
}
