export default {
  name: "MomModalTable",
  props: {
    modalProps: { type: Object, default: () => ({}) },
    modalListeners: { type: Object, default: () => ({}) },
    tableProps: { type: Object, default: () => ({}) },
    tableListeners: { type: Object, default: () => ({}) },
  },
  data() {
    return {
      selectedRows: {},
      selectedRowKeys: [],
    };
  },
  computed: {},
  methods: {
    getCompatibleSlots() {
      const repeatSlot = ["footer"];
      const reg = /(table|modal|Table|Modal)(-|_?)(.+)/;

      const scopedSlots = {
        modal: {},
        table: {},
      };

      const slotKeys = Object.keys(this.$scopedSlots);
      // eslint-disable-next-line
      for (let i = 0; i < slotKeys.length; i++) {
        const slotKey = slotKeys[i];
        const matchRes = slotKey.match(reg);
        if (!matchRes) {
          // eslint-disable-next-line
          continue;
        }
        const [origin, componentName, ...group] = matchRes;
        const slotNameAlias = group[1] || group[0];
        const slotNameCamel =
          slotNameAlias.substring(0, 1).toLocaleLowerCase() +
          slotNameAlias.substring(1);
        if (repeatSlot.includes(slotNameCamel)) {
          scopedSlots[componentName.toLocaleLowerCase()][slotNameCamel] =
            this.$scopedSlots[slotKey];
        }
      }

      return {
        modalScopedSlots: {
          ...this.$scopedSlots,
          ...scopedSlots.modal,
        },
        tableScopedSlots: {
          ...this.$scopedSlots,
          ...scopedSlots.table,
        },
      };
    },
  },
  render() {
    const { modalProps, modalListeners, tableProps, tableListeners } = this;
    const { modalScopedSlots, tableScopedSlots } = this.getCompatibleSlots();
    const momModalProps = {
      props: {
        width: modalProps.width || 1200,
        isShowFullScreen: true,
        ...modalProps,
      },
      scopedSlots: { ...modalScopedSlots },
      on: {
        ...modalListeners,
      },
    };
    const momTableProps = {
      ref: "MomTable",
      props: {
        ...tableProps,
      },
      scopedSlots: { ...tableScopedSlots },
      on: {
        ...tableListeners,
      },
    };
    return (
      <Modal {...momModalProps}>
        <MomTable {...momTableProps} />
      </Modal>
    );
  },
};
