import { getSharedPortalId } from 'utils/spaceship';
import { api } from '../constants/config';
import http, { request, RequestOptions } from '../utils/fetch';
import { UserInfo } from '@/models/user.types';
import { getScreenDataApiByPathName } from '@/utils/screen';

export async function exportScreen(id) {
  return request(api.screenExport, {
    id,
  });
}

type GetScreenDataByPathName = (params: {
  params: { screenId: number; portalId?: number };
  query?: { [k: string]: any };
  body?: string | FormData;
  options?: RequestOptions;
  userInfo?: UserInfo;
}) => Promise<any>;

export const getScreenDataByPathName: GetScreenDataByPathName = async ({
  params = {},
  query = {},
  body,
  options = {},
  userInfo = {},
}) => {
  if (!params.screenId) return;
  const portalId = getSharedPortalId();
  return request(
    getScreenDataApiByPathName(userInfo || {}),
    portalId ? { ...params, portalId } : params,
    query,
    body,
    options,
  );
};

export async function transformScreenPackage({ from, to, data }): Promise<any> {
  return http.post(api.packageTransform, { from, to, data });
}

/**
 * 给大屏绑定的引用面板添加密码
 * @param data
 * @returns
 */
export const addPasswordScreenForPanel = async (data: {
  screenPassword: string;
  screenId: number;
  panelId: number;
}) => {
  return request(api.addPasswordScreenForPanel, undefined, undefined, JSON.stringify(data));
};
