import {
  APIRequest,
  MessageAPIResponse,
  APIDecoder,
  APIJsonEncoder
} from '@ucap-webmessenger/api';
import { NoticeList } from '../models/notice-list';

export interface RetrieveNoticeRequest extends APIRequest {
  userSeq: number;
  companyCode: string;
  pageSize: number;
  pageCount: number;
}

export interface RetrieveNoticeResponse extends MessageAPIResponse {
  pageSize: number;
  pageCount: number;
  totalCount: number;

  noticeList: NoticeList[];
}

export const encodeRetrieveNotice: APIJsonEncoder<RetrieveNoticeRequest> = (
  req: RetrieveNoticeRequest
) => {
  return JSON.stringify(req);
};

export const decodeRetrieveNotice: APIDecoder<RetrieveNoticeResponse> = (
  res: any
) => {
  const noticeList: NoticeList[] = [];
  if (!!res.noticeList && res.noticeList.length > 0) {
    for (const notice of res.noticeList) {
      noticeList.push({
        ...notice,
        topYn: notice.topYn === 'Y' ? true : false
      } as NoticeList);
    }
  }

  return {
    responseCode: res.responseCode,
    responseMsg: res.responseMsg,

    pageCount: res.pageCount,
    pageSize: res.pageSize,
    totalCount: res.totalCount,

    noticeList
  } as RetrieveNoticeResponse;
};
