1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { BaseElement, element } from '../../base-ui/BaseElement';
17import { SpBubblesAIHtml } from './SpBubblesAI.html';
18import { FlagsConfig, Params } from './SpFlags';
19import { SpStatisticsHttpUtil } from '../../statistics/util/SpStatisticsHttpUtil';
20
21@element('sp-bubble-ai')
22export class SpBubblesAI extends BaseElement {
23  initElements(): void {
24    const xiaoLubanEl: HTMLElement | undefined | null = this.shadowRoot?.querySelector('#xiao-luban-help');
25    xiaoLubanEl?.addEventListener('click', () => {
26      this.xiaoLubanEvent();
27      let requestBody = {
28        action: 'AItrace',
29        event: 'AItrace'
30      };
31      SpStatisticsHttpUtil.addOrdinaryVisitAction(requestBody);
32    });
33    let isShowXiaoLuban: boolean = FlagsConfig.getFlagsConfigEnableStatus('xiaoLuBan');
34    if (isShowXiaoLuban) {
35      xiaoLubanEl?.setAttribute('enabled', '');
36    } else {
37      xiaoLubanEl?.removeAttribute('enabled');
38    }
39  }
40
41  initHtml(): string {
42    return SpBubblesAIHtml;
43  }
44
45  private xiaoLubanEvent(): void {
46    const flagConfig: Params | undefined = FlagsConfig.getFlagsConfig('AI');
47    const userId: string | undefined = flagConfig!.userId?.toString();
48    const data = {
49      'sender': `${userId}`,
50      'msgBody': 'rag SmartPerf_ad85b972',
51      'msgType': 'text',
52      'timestamp': new Date().getTime().toString(),
53      'botUser': 'p_xiaoluban',
54    };
55    fetch(`https://${window.location.host}/xiaoluban/resource`, {
56      method: 'post',
57      body: JSON.stringify(data),
58      headers: {
59        'Content-Type': 'application/json'
60      }
61    }).then(res => {
62      if (res.status === 200) {
63        window.open('im:p_xiaoluban', '_self');
64      }
65    });
66  }
67}
68
69
70