1/*
2 * Copyright (c) 2021 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#ifndef ECMASCRIPT_JS_RELATIVE_TIME_FORMAT_H
17#define ECMASCRIPT_JS_RELATIVE_TIME_FORMAT_H
18
19#include "unicode/reldatefmt.h"
20
21#include "ecmascript/intl/locale_helper.h"
22#include "ecmascript/base/number_helper.h"
23#include "ecmascript/common.h"
24#include "ecmascript/ecma_macros.h"
25#include "ecmascript/ecma_string.h"
26#include "ecmascript/ecma_vm.h"
27#include "ecmascript/global_env.h"
28#include "ecmascript/js_array.h"
29#include "ecmascript/js_date.h"
30#include "ecmascript/js_intl.h"
31#include "ecmascript/js_locale.h"
32#include "ecmascript/js_object.h"
33#include "ecmascript/js_tagged_number.h"
34#include "ecmascript/js_tagged_value.h"
35#include "ecmascript/object_factory.h"
36
37namespace panda::ecmascript {
38enum class RelativeStyleOption : uint8_t { LONG = 0x01, SHORT, NARROW, EXCEPTION };
39enum class NumericOption : uint8_t { ALWAYS = 0x01, AUTO, EXCEPTION };
40
41class JSRelativeTimeFormat : public JSObject {
42public:
43    CAST_CHECK(JSRelativeTimeFormat, IsJSRelativeTimeFormat);
44
45    static constexpr size_t LOCALE_OFFSET = JSObject::SIZE;
46    ACCESSORS(Locale, LOCALE_OFFSET, NUMBERING_SYSTEM_OFFSET)
47    ACCESSORS(NumberingSystem, NUMBERING_SYSTEM_OFFSET, ICU_FIELD_OFFSET)
48    ACCESSORS(IcuField, ICU_FIELD_OFFSET, BIT_FIELD_OFFSET)  // icu field
49    ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
50    DEFINE_ALIGN_SIZE(LAST_OFFSET);
51
52    // define BitField
53    static constexpr size_t STYLE_BITS = 3;
54    static constexpr size_t NUMERIC_BITS = 2;
55    FIRST_BIT_FIELD(BitField, Style, RelativeStyleOption, STYLE_BITS)
56    NEXT_BIT_FIELD(BitField, Numeric, NumericOption, NUMERIC_BITS, Style)
57
58    DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LOCALE_OFFSET, BIT_FIELD_OFFSET)
59    DECL_DUMP()
60
61    // 14.1.1 InitializeRelativeTimeFormat ( relativeTimeFormat, locales, options )
62    static JSHandle<JSRelativeTimeFormat> InitializeRelativeTimeFormat(
63        JSThread *thread, const JSHandle<JSRelativeTimeFormat> &relativeTimeFormat,
64        const JSHandle<JSTaggedValue> &locales, const JSHandle<JSTaggedValue> &options);
65
66    // UnwrapRelativeTimeFormat
67    static JSHandle<JSTaggedValue> UnwrapRelativeTimeFormat(JSThread *thread, const JSHandle<JSTaggedValue> &rtf);
68
69    // Get icu formatter from icu field
70    icu::RelativeDateTimeFormatter *GetIcuRTFFormatter() const
71    {
72        ASSERT(GetIcuField().IsJSNativePointer());
73        auto result = JSNativePointer::Cast(GetIcuField().GetTaggedObject())->GetExternalPointer();
74        return reinterpret_cast<icu::RelativeDateTimeFormatter *>(result);
75    }
76
77    static void FreeIcuRTFFormatter([[maybe_unused]] void *env, void *pointer, void *data)
78    {
79        if (pointer == nullptr) {
80            return;
81        }
82        auto icuFormatter = reinterpret_cast<icu::RelativeDateTimeFormatter *>(pointer);
83        icuFormatter->~RelativeDateTimeFormatter();
84        if (data != nullptr) {
85            reinterpret_cast<EcmaVM *>(data)->GetNativeAreaAllocator()->FreeBuffer(pointer);
86        }
87    }
88
89    static void ResolvedOptions(JSThread *thread, const JSHandle<JSRelativeTimeFormat> &relativeTimeFormat,
90                                const JSHandle<JSObject> &options);
91
92    static JSHandle<EcmaString> Format(JSThread *thread, double value, const JSHandle<EcmaString> &unit,
93                                       const JSHandle<JSRelativeTimeFormat> &relativeTimeFormat);
94
95    static JSHandle<JSArray> FormatToParts(JSThread *thread, double value, const JSHandle<EcmaString> &unit,
96                                           const JSHandle<JSRelativeTimeFormat> &relativeTimeFormat);
97};
98}  // namespace panda::ecmascript
99
100#endif  // ECMASCRIPT_JS_RELATIVE_TIME_FORMAT_H