import { useLoad, useShareAppMessage } from '@tarojs/taro';

import { useCallback } from 'react';

import HomePage from '@/components/home-page';
import SearchCity from '@/components/search-city';
import { GROUPS } from '@/constants/group';
import useInviteCode from '@/hooks/use-invite-code';
import { openCustomerServiceChat } from '@/utils/common';
import { getCurrentCityCode } from '@/utils/location';
import { getInviteCodeFromQueryAndUpdate } from '@/utils/partner';
import { getPageQuery } from '@/utils/route';
import { getCommonShareMessage } from '@/utils/share';
import './index.less';

const PREFIX = 'group-v2-page';

export default function GroupV2() {
  const inviteCode = useInviteCode();

  useLoad(() => {
    const query = getPageQuery();
    getInviteCodeFromQueryAndUpdate(query);
  });

  useShareAppMessage(() => getCommonShareMessage(true, inviteCode));

  const handleSelectCity = useCallback(cityCode => {
    const group = GROUPS.find(g => String(g.cityCode) === cityCode);
    if (group) {
      openCustomerServiceChat(group.serviceUrl);
    }
  }, []);

  return (
    <HomePage>
      <div className={PREFIX}>
        <SearchCity
          onSelectCity={handleSelectCity}
          currentCity={getCurrentCityCode()}
          forGroup
          offset={72}
          banner="点击城市加入本地通告群，高薪工作早知道"
        />
      </div>
    </HomePage>
  );
}
