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
16#include "contacts_napi_utils.h"
17
18namespace OHOS {
19namespace ContactsApi {
20static constexpr const char *JS_ERROR_INVALID_INPUT_PARAMETER_STRING =
21    "parameter error. Mandatory parameters are left unspecified.";
22static constexpr const char *JS_ERROR_VERIFICATION_FAILED_PARAMETER_STRING =
23    "parameter error. The type of id must be number.";
24static constexpr const char *JS_ERROR_PERMISSION_DENIED_STRING = "Permission denied";
25napi_value ContactsNapiUtils::ToInt32Value(napi_env env, int32_t value)
26{
27    napi_value staticValue = nullptr;
28    napi_create_int32(env, value, &staticValue);
29    return staticValue;
30}
31
32napi_value ContactsNapiUtils::CreateClassConstructor(napi_env env, napi_callback_info info)
33{
34    napi_value thisArg = nullptr;
35    void *data = nullptr;
36    napi_get_cb_info(env, info, nullptr, nullptr, &thisArg, &data);
37    napi_value global = nullptr;
38    napi_get_global(env, &global);
39    return thisArg;
40}
41
42bool ContactsNapiUtils::MatchValueType(napi_env env, napi_value value, napi_valuetype targetType)
43{
44    napi_valuetype valueType = napi_undefined;
45    napi_typeof(env, value, &valueType);
46    return valueType == targetType;
47}
48
49bool ContactsNapiUtils::MatchParameters(
50    napi_env env, const napi_value parameters[], std::initializer_list<napi_valuetype> valueTypes)
51{
52    if (parameters == nullptr) {
53        return false;
54    }
55    int i = 0;
56    for (auto beg = valueTypes.begin(); beg != valueTypes.end(); ++beg) {
57        if (!MatchValueType(env, parameters[i], *beg)) {
58            return false;
59        }
60        ++i;
61    }
62    return true;
63}
64
65napi_value ContactsNapiUtils::CreateError(napi_env env, int32_t err)
66{
67    napi_value businessError = nullptr;
68    napi_value errorCode = nullptr;
69    napi_value errorMessage = nullptr;
70    if (err == PERMISSION_ERROR) {
71        napi_create_string_utf8(env, JS_ERROR_PERMISSION_DENIED_STRING, NAPI_AUTO_LENGTH, &errorMessage);
72    }
73    if (err == PARAMETER_ERROR) {
74        napi_create_string_utf8(env, JS_ERROR_INVALID_INPUT_PARAMETER_STRING, NAPI_AUTO_LENGTH, &errorMessage);
75    }
76    napi_create_int32(env, err, &errorCode);
77    napi_create_error(env, nullptr, errorMessage, &businessError);
78    napi_set_named_property(env, businessError, "code", errorCode);
79    return businessError;
80}
81
82napi_value ContactsNapiUtils::CreateErrorByVerification(napi_env env, int32_t err)
83{
84    napi_value businessError = nullptr;
85    napi_value errorCode = nullptr;
86    napi_value errorMessage = nullptr;
87    if (err == PERMISSION_ERROR) {
88        napi_create_string_utf8(env, JS_ERROR_PERMISSION_DENIED_STRING, NAPI_AUTO_LENGTH, &errorMessage);
89    }
90    if (err == PARAMETER_ERROR) {
91        napi_create_string_utf8(env, JS_ERROR_VERIFICATION_FAILED_PARAMETER_STRING, NAPI_AUTO_LENGTH, &errorMessage);
92    }
93    napi_create_int32(env, err, &errorCode);
94    napi_create_error(env, nullptr, errorMessage, &businessError);
95    napi_set_named_property(env, businessError, "code", errorCode);
96    return businessError;
97}
98} // namespace ContactsApi
99} // namespace OHOS
100