#  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.

Namespaces:
  =: io.murano.resources
  std: io.murano
  sys: io.murano.system

Name: MetadataAware

Properties:
  checkApplicability:
    Contract: $.bool().notNull()
    Default: true

Methods:
  .init:
    Body:
      - $this._metadefBrowsers: {}

  getMetadefBrowser:
    Arguments:
      - region:
          Contract: $.class(std:CloudRegion).notNull()
    Body:
      - $browser: $this._metadefBrowsers.get($region.name)
      - If: $browser = null
        Then:
          - $browser: new(sys:MetadefBrowser, $region)
          - $this._metadefBrowsers[$region.name]: $browser
      - Return: $browser

  getMetadata:
    Arguments:
      - region:
          Contract: $.class(std:CloudRegion).notNull()
    Body:
      - $thisMeta: metadata($this) or {}
      - $parentsMeta: {}
      - $p: $this.find(std:Object)
      - While: $p != null
        Do:
          - $pmeta: metadata($p) or {}
          - $pmeta: dict($pmeta.items().where(not $[0] in $parentsMeta.keys()))
          - $parentsMeta: $parentsMeta.set($pmeta)
          - $p: $p.find(std:Object)
      - $resourceType: $this.getResourceType()
      - If: $this.checkApplicability and $resourceType
        Then:
          - $browser: $this.getMetadefBrowser($region)
          - $parentsMeta: dict($parentsMeta.items().where($browser.canBeAppliedTo($[0], $resourceType)))
      - Return: $parentsMeta.set($thisMeta)

  getResourceType:

