<!--
 Copyright (C) 2016-2023 Jones Magloire @Joxit

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
-->
<image-date>
  <div title="Creation date { getLocalDate(props.image) }">{ getDate(props.image) }</div>
  <script>
    import { dateFormat } from '../../scripts/utils';
    export default {
      onMounted(props) {
        this.loadCreationDate(props);
      },
      onUpdated(props) {
        this.loadCreationDate(props);
      },
      loadCreationDate(props) {
        if (!props.image.creationDate && !props.image.ociImage) {
          props.image.one('creation-date', (date) => {
            this.update({
              date: date,
              localDate: date && date.toLocaleString(),
            });
          });
          props.image.trigger('get-date');
        }
      },
      getDate(image) {
        return !image.ociImage ? `${dateFormat(image.creationDate)} ago` : 'Not Available';
      },
      getLocalDate(image) {
        return !image.ociImage
          ? (image.creationDate && image.creationDate.toLocaleString()) || 'unknown'
          : 'unavailable on OCI index/Buildkit export cache';
      },
    };
  </script>
</image-date>
