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 */
15import common from '../data/commonData';
16import HiLog from './HiLog';
17import call from '@ohos.telephony.call';
18
19const TAG = 'TelephoneUtil';
20/**
21 *  log package tool class
22 */
23const infoMegTelephone = [
24    '1065796709202', '1065502043202', '1065902090202', '1069055999202',
25    '106579670915', '106550204315', '106590209015', '106905599915',
26    '106', '400', '111', '100', '118', '116', '12306', '12329', '12345',
27    '12122', '12321', '12580', '12520', '12583', '02512329', '053287003810',
28    '1258319559899', '1019', '12583110086', '000000', '95', '96',
29];
30
31export default {
32
33    /**
34     * Check whether the mobile number is notification information.
35     * @param telephone
36     * @return Yes or no
37     */
38    judgeIsInfoMsg(telephone: string): boolean {
39        let result: boolean = false;
40        if (telephone == null || telephone == common.string.EMPTY_STR) {
41            return result;
42        }
43        for (let item of infoMegTelephone) {
44            if (telephone.indexOf(item) == 0) {
45                result = true;
46                break;
47            }
48        }
49        return result;
50    },
51
52    formatTelephone(telephone) {
53        let formatTelephone: string = telephone;
54        if (telephone == null || telephone == common.string.EMPTY_STR) {
55            formatTelephone = common.string.EMPTY_STR;
56        }
57        if (formatTelephone.startsWith('+86')) {
58            formatTelephone = formatTelephone.substring(3);
59        } else if(formatTelephone.startsWith('86')) {
60            formatTelephone = formatTelephone.substring(2);
61        }
62        return formatTelephone;
63    },
64
65    dealSelectContactsSort(selectContacts) {
66        if (selectContacts.length == 0) {
67            return;
68        }
69        let result = [];
70        let telephone = common.string.EMPTY_STR;
71        let contactsMap = new Map();
72        for (let item of selectContacts) {
73            telephone = telephone + item.telephone + common.string.COMMA;
74            if (!contactsMap.has(item.telephone)) {
75                contactsMap.set(item.telephone, item);
76            }
77        }
78        telephone = this.dealTelephoneSort(telephone.substring(0, telephone.length - 1));
79        let telephones = telephone.split(common.string.COMMA);
80        for (let element of telephones) {
81            if (contactsMap.has(element)) {
82                result.push(contactsMap.get(element));
83            }
84        }
85        return result;
86    },
87
88    /**
89     * Mobile number, sorted in ascending order.
90     * @param telephone
91     * @return
92     */
93    dealTelephoneSort(telephone) {
94        if (telephone == null || telephone == common.string.EMPTY_STR) {
95            return common.string.EMPTY_STR;
96        }
97        let result = common.string.EMPTY_STR;
98        let telephones = telephone.split(common.string.COMMA);
99        // If there is only one mobile number, no sorting is required.
100        if (telephones.length == 1) {
101            return telephone;
102        }
103        let telephoneMap = new Map();
104        let indexs = [];
105        let count = 0;
106        // grouping
107        for (let item of telephones) {
108            if (telephoneMap.has(item.length)) {
109                let strings = telephoneMap.get(item.length);
110                strings.push(item);
111            } else {
112                let strings = [];
113                strings.push(item);
114                telephoneMap.set(item.length, strings);
115                indexs[count++] = item.length;
116            }
117        }
118        // Sort from Large to Small
119        this.bubbleSort(indexs, count);
120        for (let index of indexs) {
121            let arrs = telephoneMap.get(index);
122            this.bubbleSort(arrs, arrs.length);
123            for (let arr of arrs) {
124                result = result + arr + common.string.COMMA;
125            }
126        }
127        // Obtain the corresponding results and pair them.
128        return result.substring(0, result.length - 1);
129    },
130
131    /**
132     * Bubble sort, sorted in ascending order.
133     * @param arr
134     * @param length
135     * @return
136     */
137    bubbleSort(arr, length) {
138        // A minimum value is generated from the back to the front at a time, and the final position of a number
139        // in the sequence can be determined at a time.
140        for (let i = 0; i < length - 1; i++) {
141            // Improvement of bubbling so that the sequence is ordered if no reversal occurs in one pass
142            for (let j = length - 1; j > i; j--) {
143                // A minimum value pops up from the back at a time.
144                if (arr[j] < arr[j - 1]) {
145                    // Reverse order occurs, then swap
146                    let temple = arr[j];
147                    arr[j] = arr[j - 1];
148                    arr[j - 1] = temple;
149                }
150            }
151        }
152    },
153    async formatPhoneNumber(phoneNumber) {
154        if (phoneNumber == null || phoneNumber === common.string.EMPTY_STR) {
155            HiLog.w(TAG, 'formatPhoneNumber, param is null');
156            return common.string.EMPTY_STR;
157        }
158        let promise = call.formatPhoneNumber(phoneNumber);
159        let formatPhoneNumber = common.string.EMPTY_STR;
160        promise.then((value) => {
161            formatPhoneNumber = value;
162        }).catch((err) => {
163            HiLog.e(TAG, 'formatPhoneNumber, error: ' + JSON.stringify(err.message));
164        });
165        await promise;
166        return formatPhoneNumber;
167    }
168};