import { axios } from ".";

const baseUrl = "/api";
// const baseUrl = 'http://127.0.0.1:8021'
export const expandApi = "/expandApi";

export function getNews(v: string) {
  return fetch(baseUrl + "/v1/article/index" + "?cid=8&language=" + v, {
    method: "GET",
    headers: {
      "content-type": "application/json",
    },
  }).then((ret) => {
    return ret.json();
  });
}

export function getNewsDetails(id: number, v: string) {
  return fetch(baseUrl + `/v1/article/view?id=${id}&language=${v}`, {
    method: "GET",
    headers: {
      "content-type": "application/json",
    },
  }).then((ret) => {
    return ret.json();
  });
}

export function getAccountRecords(
  addr: string,
  ranges: number[],
  symbol: string
) {
  const list = ranges.map((i) => {
    return {
      page: {
        size: 1,
        number: 1,
      },
      range: [
        {
          end: i,
          key: "block_time",
        },
      ],
      sort: [
        {
          ascending: false,
          key: "height_index",
        },
      ],
      match: [
        {
          key: "address",
          value: addr,
        },
        {
          key: "asset_exec",
          value: "coinsx",
        },
        {
          key: "asset_symbol",
          value: symbol,
        },
      ],
    };
  });

  return axios(expandApi, {
    method: "post",
    params: {
      id: 1,
      method: "Account.AccountRecords",
      params: [list],
    },
  });
}

export function getTxCount() {
  return axios(expandApi, {
    method: "post",
    params: {
      id: 1,
      method: "Tx.TxCount",
      params: [],
    },
  });
}

export function getTxList(number: number, size: number) {
  return axios(expandApi, {
    method: "post",
    params: {
      id: 1,
      method: "Tx.TxList",
      params: [
        {
          sort: [
            {
              key: "height",
              ascending: false,
            },
          ],
          page: {
            number,
            size,
          },
        },
      ],
    },
  });
}

export function blockStatStatGets(startHeight: number, endHeight: number) {
  return axios(expandApi, {
    method: "post",
    params: {
      id: 1,
      method: "BlockStat.StatGets",
      params: [
        {
          height: [startHeight, endHeight],
        },
      ],
    },
  });
}

export function getAccountCount() {
  return axios(expandApi, {
    method: "post",
    params: {
      id: 1,
      method: "Account.Count",
      params: [
        {
          match: [],
        },
      ],
    },
  });
}

export function statSearch(times: number[]) {
  return axios(expandApi, {
    method: "post",
    params: {
      id: 1,
      method: "BlockStat.StatSearches",
      params: [
        times.map((i) => {
          return {
            page: {
              size: 1,
              number: 1,
            },
            range: [
              {
                start: i,
                key: "time",
              },
            ],
            sort: [
              {
                ascending: true,
                key: "height",
              },
            ],
          };
        }),
      ],
    },
  });
}

export function getAddressTxCount(addr: string) {
  return axios(expandApi, {
    method: "post",
    params: {
      id: 1,
      method: "Tx.TxCount",
      params: [
        {
          match_one: [
            {
              key: "from",
              value: addr,
            },
            {
              key: "to",
              value: addr,
            },
          ],
          match: [],
        },
      ],
    },
  });
}

export function getAllPos33TicketCount() {
  return axios("/yccApi", {
    method: "post",
    params: {
      method: "pos33.GetAllPos33TicketCount",
      params: [{}],
    },
  });
}

export function getTxCountBewteenTimes(
  startUnixTime: number,
  endUnixTime: number
) {
  return axios(expandApi, {
    method: "post",
    params: {
      id: 1,
      method: "Tx.TxCount",
      params: [
        {
          range: [
            {
              start: startUnixTime,
              end: endUnixTime,
              key: "block_time",
            },
          ],
        },
      ],
    },
  });
}

export function getAddrTxList(addr: string, number: number, size: number) {
  return axios(expandApi, {
    method: "post",
    params: {
      id: 1,
      method: "Tx.TxList",
      params: [
        {
          match_one: [
            { key: "from", value: addr },
            { key: "to", value: addr },
          ],
          match: [],
          sort: [{ key: "height", ascending: false }],
          page: { number, size },
        },
      ],
    },
  });
}

export function getMakerAddrCount(addr: string) {
  return axios(expandApi, {
    method: "post",
    params: {
      id: 1,
      method: "Tx.TxCount",
      params: [
        {
          match: [
            {
              key: "maker_addr",
              value: addr,
            },
          ],
        },
      ],
    },
  });
}
export function getVoterAddrCount(addr: string) {
  return axios(expandApi, {
    method: "post",
    params: {
      id: 1,
      method: "Tx.TxCount",
      params: [
        {
          match: [
            {
              key: "voter_addr",
              value: addr,
            },
          ],
        },
      ],
    },
  });
}

export function getMakerAddr(addr: string, number: number, size: number) {
  return axios(expandApi, {
    method: "post",
    params: {
      id: 1,
      method: "Tx.TxList",
      params: [
        {
          sort: [
            {
              key: "height",
              ascending: false,
            },
          ],
          match: [
            {
              key: "maker_addr",
              value: addr,
            },
          ],
          page: {
            number,
            size,
          },
        },
      ],
    },
  });
}

export function getVoterAddr(addr: string, number: number, size: number) {
  return axios(expandApi, {
    method: "post",
    params: {
      id: 1,
      method: "Tx.TxList",
      params: [
        {
          sort: [
            {
              key: "height",
              ascending: false,
            },
          ],
          match: [
            {
              key: "voter_addr",
              value: addr,
            },
          ],
          page: {
            number,
            size,
          },
        },
      ],
    },
  });
}
