<template>
  <DockAssignment v-bind="$props" @goToRoute="$emit('goToRoute', $event)">
    <template v-slot:stopToDockInfo="{ stop }">
      <slot name="stopToDockInfo" :stop="stop" />
    </template>
  </DockAssignment>
</template>

<script>
import { language as fieldeasLanguage } from "fieldeas-web";
import Vue from "vue";
import global from "../mixins/Global";
import i18n from "./plugins/language";
import dockAssignment from "./store/modules/dockAssignment.store";

import dockAssignmentComponent from "./components/DockAssignment/index.vue";

const MAPPING = {
  [fieldeasLanguage.Spanish]: "es-ES",
  [fieldeasLanguage.English]: "en-GB",
  [fieldeasLanguage.French]: "en-GB",
  [fieldeasLanguage.Russian]: "en-GB",
  [fieldeasLanguage.German]: "en-GB",
  [fieldeasLanguage.Italian]: "en-GB",
  [fieldeasLanguage.Portuguese]: "pt-PT",
  [fieldeasLanguage.Catalan]: "en-GB",
};

export default {
  name: "fieldeas-docks-assignment",
  props: {
    client: {
      default: null,
    },
    token: {
      default: null,
    },
    user: {
      default: null,
    },
    showGoToRoute: {
      default: true,
    },
    showButtonBack: {
      type: Boolean,
      default: true,
    },
    showDockDetail: {
      default: true,
    },
    clientAddScheduleRestrictions: {
      default: null,
    },
    clientCheckDockMaxStops: {
      default: null,
    },
    clientCheckRestrictions: {
      default: null,
    },
    clientConcurrentlyStops: {
      default: null
    },
    operationID: {
      default: null,
    },
    clientMaxOperarios: {
      default: null
    },
    clientCosteOperarios: {
      default: null
    },
    clientSlotDuration: {
      // default: '00:05:00'
      default: null
    }
  },
  components: {
    DockAssignment: dockAssignmentComponent,
  },
  mixins: [global],
  async beforeCreate() {
    if (this.$store && !this.$store.hasModule("dockAssignment")) {
      this.$store.registerModule("dockAssignment", dockAssignment);
    }
  },
  beforeMount() {
    const fieldeasDocksOptions = {
      client: this.client,
      token: this.token,
      user: this.user,
    };

    if (this.$i18n) {
      const currentTranslations =
        this.$i18n.messages[MAPPING[this.user.Language]] != null
          ? Object.keys(this.$i18n.messages[MAPPING[this.user.Language]])
          : [];
      const translationsToMerge = Object.assign({}, i18n[this.user.Language]);

      for (const key in translationsToMerge) {
        if (
          Object.hasOwnProperty.call(translationsToMerge, key) &&
          currentTranslations.includes(key)
        ) {
          delete translationsToMerge[key];
        }
      }

      this.$i18n.mergeLocaleMessage(
        MAPPING[this.user.Language],
        translationsToMerge
      );
    }

    this.$store.commit("_setDocksAssignmentUser", this.user);
    this.$store.commit("_setDocksAssignmentToken", this.token);
    this.$store.commit("_setDocksAssignmentClient", this.client);

    if (this.operationID) {
      this.$store.commit("_setDocksAssignmentOperation", this.operationID);
      this.$store.dispatch("fetchPlatformByOperation")
    }

    this.$store.commit("_setDocksAssignmentOptions", fieldeasDocksOptions);
  },
};
</script>