/*
 * Copyright (c) 2026 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import * as idl from "@idlizer/core/idl"
import { isRoot } from "@idlizer/core"
import { MakeSelector, OhosSeed, OhosRole } from "@idlizer/libohos"
import { attributeProducer, componentProducer, peerProducer } from "./managed/attribute.js"
import { propertyProducer } from "./managed/property.js"
import { interfaceProducer } from "./managed/interface.js"
import { optionsProducer } from "./managed/callable.js"

type ArkUISpecificRole<N extends idl.IDLNode> =
  N extends idl.IDLInterface ? 'component' | 'peer' :
  N extends idl.IDLProperty ? 'peer' :
  N extends idl.IDLCallable ? 'peer' :
  never

export type ArkUIRole<N extends idl.IDLNode> = OhosRole<N> | ArkUISpecificRole<N>

export type ArkUISeed<N extends idl.IDLNode> = OhosSeed<N, ArkUIRole<N>>

export const producers = [
  attributeProducer,
  peerProducer,
  componentProducer,
  propertyProducer,
  interfaceProducer,
  optionsProducer
]

export function registerArkUIProducers(selector: MakeSelector) {
  for (const producer of producers) {
    selector.register(producer as any)
  }
}

export function isComponentAttribute(node: idl.IDLInterface) {
  return isRoot(node.name) ||
    idl.hasExtAttribute(node, idl.IDLExtendedAttributes.Component)
}

export function isAttributeModifier(prop: idl.IDLProperty) {
  return prop.name === 'attributeModifier'
}