import { getCardList } from './service';
import { CREATE_CARD_TYPE } from './common';
import { industryEntry } from "../../../../core/utils";
export function promisify(func) {
  return function wrapPromisify(args) {
    return new Promise(resolve => {
      func({ ...args,

        success(res) {
          resolve([null, res]);
        },

        fail(err) {
          resolve([err, null]);
        }

      });
    });
  };
} // 查看pdf

export function openPdf({
  url
}) {
  my.showLoading();
  my.downloadFile({
    // 示例 url，并非真实存在
    url,

    success({
      apFilePath
    }) {
      my.openDocument({
        filePath: apFilePath,
        fileType: 'pdf'
      });
    },

    fail: () => {
      my.showToast({
        type: 'none',
        content: '查看失败'
      });
    },
    complete: () => {
      my.hideLoading();
    }
  });
} // 替换 ***
// str: 要替换的字符串
// start: 前面保留几位
// end: 后面保留几位

export function replaceStar(str = '', start = 0, end = 0) {
  if (!str) {
    return '';
  }

  const len = str.length;

  if (len <= start + end) {
    return str;
  } // eslint-disable-next-line no-shadow


  return [...str].reduce((str, value, i) => i < start || i > len - end - 1 ? str + value : `${str}*`, '');
} // 页面跳转，方便后面迁移

export function navigateTo({
  url,
  ...other
}) {
  my.navigateTo({
    url: `${industryEntry}/hospital${url}`,
    ...other
  });
} // 字符串转对象

export function mapToArray(obj) {
  let parseObj;

  try {
    parseObj = JSON.parse(obj);
  } catch (error) {
    parseObj = {};
  }

  return Object.keys(parseObj || {}).map(item => ({
    key: item,
    value: parseObj[item]
  }));
} // 获取查询信息

export async function getCardListRange() {
  const [, result] = await getCardList({
    type: 0,
    pageIndex: 1,
    pageSize: 100
  });

  if (result) {
    return result.list.map(item => ({
      id: item.cardNum,
      name: `${item.name}-${item.cardNum}`,
      isDefault: item.isDefault
    }));
  }

  return [];
} // 跳转到创建就诊卡

export const openCardForm = async item => {
  const [, bookingStep] = item.urlQuery.split('=');
  const [, pageUuid] = item.url.split(':');

  if (bookingStep === 'bindCard') {
    history.openPage({
      pageType: 'card-form',
      data: {
        type: CREATE_CARD_TYPE.BIND,
        btnText: '绑定就诊卡',
        back: true,
        pageUuid
      }
    });
  }

  if (bookingStep === 'createCard') {
    history.openPage({
      pageType: 'card-form',
      data: {
        type: CREATE_CARD_TYPE.CREATE,
        btnText: '创建电子就诊卡',
        back: true,
        pageUuid
      }
    });
  }
}; // 页面跳转

export const history = (() => {
  const storage = ({
    pageType,
    data
  }) => {
    if (!getApp().globalData.pageTemp) {
      getApp().globalData.pageTemp = {};
    }

    getApp().globalData.pageTemp[pageType] = data;
  };

  return {
    openPage({
      pageType,
      data
    }) {
      storage({
        pageType,
        data
      });
      navigateTo({
        url: `/pages/page-temp/index?pageType=${pageType}`
      });
    },

    replacePage({
      pageType,
      data
    }) {
      storage({
        pageType,
        data
      });
      my.redirectTo({
        url: `${industryEntry}/hospital/pages/page-temp/index?pageType=${pageType}`
      });
    }

  };
})();