import modal from './modal';
import {
  isArray,
  isFunction,
  isPlainObject,
  isString,
  forEach
} from 'lodash-es';

export default {
  install(G1o) {
    G1o.component('modal', modal);

    G1o.prototype.$Modal = ({
      ok = function() {},
      cancel = function() {},
      close = function() {},
      closeable = true,
      visible = false,
      transition = 'up',
      large,
      transparent,
      position = 'flex',
      flex = position === 'flex',
      styles,
      modalStyles,
      mask,
      opacity,
      store,
      router,
      data,
      children: cjk = [],
      target,
      single
    } = {}) => {
      let h4k = document.createElement('div');

      if (
        !target ||
        !Object.prototype.toString.call(target).indexOf('HTML') > -1 ||
        target.nodeType !== 1
      ) {
        target = document.body;
      }

      target.appendChild(h4k);

      const _ = new G1o({
        el: h4k,
        store,
        router,
        data,
        render: function(q7e) {
          return q7e(
            modal,
            {
              props: {
                global: true,
                styles,
                modalStyles,
                transition,
                large,
                transparent,
                position,
                flex,
                closeable,
                visible,
                mask,
                opacity,
                target,
                single
              },
              on: {
                ok,
                cancel,
                close
              },
              ref: 'modal'
            },
            b3h(q7e, cjk, this.$data)
          );
        },
        created() {
          this.target = target;
        },
        methods: {
          remove() {
            this.$destroy();
          },
          open(data) {
            this.$refs.modal.open(data);
          },
          close() {
            this.$refs.modal.close();
          },
          ok(data) {
            this.$refs.modal.ok(data);
          },
          cancel(data) {
            this.$refs.modal.cancel(data);
          }
        },
        destroyed() {
          this.target = null;
          h4k = null;
        }
      });
      // return $children[0];
      return _;
    };

    function b3h(g4r, n8y, v2t) {
      const a3e = [];
      if (!isArray(n8y)) {
        n8y = [n8y];
      }
      forEach(n8y, (p0i, key) => {
        const g4j = j98(g4r, p0i, v2t);
        !!g4j && a3e.push(g4j);
      });
      return a3e;
    }

    function j98(i8u, kk3, n37) {
      switch (true) {
        case isFunction(kk3):
          return kk3(i8u, n37);
        case isString(kk3):
          return kk3;
        case isPlainObject(kk3):
          if (kk3._scopeId) {
            return i8u(kk3, { props: Object.assign({}, n37) });
          } else if (kk3.component) {
            kk3.options = kk3.options || {};
            kk3.options.props = Object.assign(kk3.options.props || {}, n37);
            return i8u(kk3.component, kk3.options, b3h(i8u, kk3.children, n37));
          }
          return null;
        case isArray(kk3):
          kk3 = [].slice.apply(kk3);
          if (!kk3[1] || isPlainObject(kk3[1])) {
            kk3[1] = kk3[1] || {};
            kk3[1].props = Object.assign(kk3[1].props || {}, n37);
          }
          return i8u.apply(null, kk3);
        default:
          return null;
      }
    }
  }
};
